Mongoose $push 无法将对象推送到正确的文档中
Mongoose $push cannot push object into correct document
我有一个像这样的猫鼬模式:
A = {
_id: Schema.Types.ObjectId,
arrayA:[{
_id,
nestedArray: [Schema.Types.ObjectId]
}],
arrayB: [Schema.Types.ObjectId]
}
我想将对象 ID 推入特定 arrayA 对象中的 nestedArray 并且
arrayB 应通过以下代码包含特定的对象 ID:
A.update({'arrayA._id': arrayAId, arrayB: {$in: [arrayContainsSomeArrayBIds]}},
{$push: {'arrayA.$.nestedArray': nestedArrayId}}, function(err) {
});
但是,对象 ID 被推入 arrayA 中最后一个对象的 nestedArray。
如果删除 arrayB: {$in: [arrayContainsSomeArrayBIds]}
,对象 ID 可以被推入 arrayA 中的正确对象。
猫鼬版本:3.8.21
谁能帮我找出问题所在?
目前在 MongoDB
中无法使用 positional
运算符更新数组元素,当查询文档包含对除正在更新的数组之外的其他数组的引用时。
下面的代码包含对两个数组字段的引用:arrayA
和 arrayB
,当
更新发布于 arrayA
。这是无效的,会导致不良行为。
A.update({'arrayA._id': arrayAId, arrayB: {$in: [arrayContainsSomeArrayBIds]}},
{$push: {'arrayA.$.nestedArray': nestedArrayId}}, function(err) {
});
来自docs,
Only one array field may appear in the query document.
The query document should only contain a single condition on the array field
being projected.
Multiple conditions may override each other
internally and lead to undefined behavior.
Under these requirements,
the following query is incorrect:
db.collection.find( { <array>: <value>, <someOtherArray>: <value2> },
{ "<array>.$": 1 } )
解决方案是修改您的代码以触发两个查询:
- 获取符合我们条件的
_ids
个文档
- 然后执行更新。
示例代码流程:
A.find({'arrayA._id': arrayAId, arrayB: {$in: [arrayContainsSomeArrayBIds]}},
function(err,data){
data.forEach(function(doc){
A.update({'arrayA._id': arrayAId,
"_id":doc._id},
{$push: {'arrayA.$.nestedArray': nestedArrayId}},
function(err) {
});
})
});
我有一个像这样的猫鼬模式:
A = {
_id: Schema.Types.ObjectId,
arrayA:[{
_id,
nestedArray: [Schema.Types.ObjectId]
}],
arrayB: [Schema.Types.ObjectId]
}
我想将对象 ID 推入特定 arrayA 对象中的 nestedArray 并且 arrayB 应通过以下代码包含特定的对象 ID:
A.update({'arrayA._id': arrayAId, arrayB: {$in: [arrayContainsSomeArrayBIds]}},
{$push: {'arrayA.$.nestedArray': nestedArrayId}}, function(err) {
});
但是,对象 ID 被推入 arrayA 中最后一个对象的 nestedArray。
如果删除 arrayB: {$in: [arrayContainsSomeArrayBIds]}
,对象 ID 可以被推入 arrayA 中的正确对象。
猫鼬版本:3.8.21
谁能帮我找出问题所在?
目前在 MongoDB
中无法使用 positional
运算符更新数组元素,当查询文档包含对除正在更新的数组之外的其他数组的引用时。
下面的代码包含对两个数组字段的引用:arrayA
和 arrayB
,当
更新发布于 arrayA
。这是无效的,会导致不良行为。
A.update({'arrayA._id': arrayAId, arrayB: {$in: [arrayContainsSomeArrayBIds]}},
{$push: {'arrayA.$.nestedArray': nestedArrayId}}, function(err) {
});
来自docs,
Only one array field may appear in the query document. The query document should only contain a single condition on the array field being projected. Multiple conditions may override each other internally and lead to undefined behavior. Under these requirements, the following query is incorrect:
db.collection.find( { <array>: <value>, <someOtherArray>: <value2> },
{ "<array>.$": 1 } )
解决方案是修改您的代码以触发两个查询:
- 获取符合我们条件的
_ids
个文档 - 然后执行更新。
示例代码流程:
A.find({'arrayA._id': arrayAId, arrayB: {$in: [arrayContainsSomeArrayBIds]}},
function(err,data){
data.forEach(function(doc){
A.update({'arrayA._id': arrayAId,
"_id":doc._id},
{$push: {'arrayA.$.nestedArray': nestedArrayId}},
function(err) {
});
})
});