本文介绍了mongodb 中的数组过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下架构.
{
posts: [
{
_id: '5ayuunbdsyuuuyut778'
replies: [{
_id: "67hfudj7e9whduu888",
text: "something"
}]
}
]
}
我想更新文字,特别是回复.我正在使用猫鼬.
I want to update the text in particular reply.I am using mongoose.
我已将查询编写如下
Post.findOneAndUpdate(
{'posts.replies._id': _id},
{$set: {'posts.$[post].replies.$[reply].text': "something1"}},
{ arrayFilters: [{'post._id': postId}, { 'reply._id': _id }]}
)
此查询未更新文档.
我错过了什么吗?我是否需要使用 ObjectId 来转换 id
Am I missing something? Do I need to cast ids using ObjectId
推荐答案
您需要使用 new: true
来获取更新的值并将 id 转换为 mongoose objectId 以使其工作
You need to use new: true
to get the updated value and cast id to mongoose objectId to make it work
Post.findOneAndUpdate(
{ 'posts.replies._id': _id },
{ $set: { 'posts.$[post].replies.$[reply].text': "something1" } },
{ arrayFilters: [{ 'post._id': postId }, { 'reply._id': _id }], new: true }
)
这篇关于mongodb 中的数组过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!