Node.js Mongoose使用FindUpdate合并数组
我想通过Node.js Mongoose使用FindUpdate合并数组,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我想通过Schema.findByIdAndUpdate(…)将数组附加到mongoose中的现有数组。它应该是这样的: Schema.findByIdAndUpdate(id, { $merge: { existingArray: otherArray } }, (...)) { ids: [1,2,3,4] ... } { ids: [1,2,3,4,5,6,7,8] ... } 示例 如果数据库中有一个如下所示的文档: Schema.findByIdA
Schema.findByIdAndUpdate(…)
将数组附加到mongoose中的现有数组。它应该是这样的:
Schema.findByIdAndUpdate(id, { $merge: { existingArray: otherArray } }, (...))
{
ids: [1,2,3,4]
...
}
{
ids: [1,2,3,4,5,6,7,8]
...
}
示例
如果数据库中有一个如下所示的文档:
Schema.findByIdAndUpdate(id, { $merge: { existingArray: otherArray } }, (...))
{
ids: [1,2,3,4]
...
}
{
ids: [1,2,3,4,5,6,7,8]
...
}
我想将此文档更新为如下所示:
Schema.findByIdAndUpdate(id, { $merge: { existingArray: otherArray } }, (...))
{
ids: [1,2,3,4]
...
}
{
ids: [1,2,3,4,5,6,7,8]
...
}
借助此阵列:
[5,6,7,8]
有适合我的运算符吗?您可以使用带修饰符的运算符
像这样的
db.collection.findOneAndUpdate(
{ "_id" : id },
{ "$push": { "ids": { "$each": [5,6,7,8] }}}
)
你能进一步解释你的问题吗?我已经添加了一个例子,希望这有帮助:DIs立即更新?mongoose我的版本是5.10.3,使用findOneAndUpdate。嵌套数组对象内部数组对象需要推送您有什么想法吗?