[英]$pull array embedded docs MongoDB
this is my problem, I need to remove a specific _id charge reference into array members.charges from specific member.id into a embedded doc. 这是我的问题,我需要将特定的_id费用引用删除到数组member.charges中,并将其从特定的member.id删除到嵌入式文档中。
(I hope be clear about my issue). (我希望我的问题很清楚)。
I try with this. 我尝试这个。
db.branches.update(
{'members._id':ObjectId("565dc20d338b60720c25e8d0"),
'members.charges': ObjectId("565dc20d338b60720c25e8d1")
},
{
$pull:{'members.charges': ObjectId("565dc20d338b60720c25e8d1") }
}
);
(branches, is the name of the collection). (分支,是集合的名称)。
But mongoDB says: 但是mongoDB说:
cannot use the part (members of members.charges) to traverse the element. 无法使用零件(members.charges的成员)遍历元素。
My collection structure is: 我的收藏结构是:
{
"_id" : ObjectId("565dc1cb338b60720c25e8ce"),
"phone" : "33 2132 1321",
"address" : "whereever",
"name" : "Gym2",
"account_id" : ObjectId("565dc1ad338b60720c25e8cc"),
"visits" : [],
"members" : [
{
"name" : "Test User",
"package" : ObjectId("565dc1f9338b60720c25e8cf"),
"address" : "Sur 113 A No 429",
"phone" : "32 1321 3232",
"contactPhone" : "32 3213 1231",
"email" : "test@gmail.com",
"pendingCharges" : true,
"_id" : ObjectId("565dc20d338b60720c25e8d0"),
"created" : ISODate("2015-12-01T15:51:41.187Z"),
"charges" : [
ObjectId("565dc20d338b60720c25e8d1")
],
"active" : true
},
{
"name" : "Test user 2",
"package" : ObjectId("565dc1f9338b60720c25e8cf"),
"address" : "whereever",
"phone" : "32 1321 3232",
"contactPhone" : "32 3213 1231",
"email" : "uodsaanduco@gmail.com",
"pendingCharges" : true,
"_id" : ObjectId("565dc22b338b60720c25e8d2"),
"created" : ISODate("2015-12-01T15:52:11.581Z"),
"charges" : [
ObjectId("565dc22b338b60720c25e8d3")
],
"active" : true
}
],
"packages" : [
ObjectId("565dc1f9338b60720c25e8cf")
]
}
You need to tell the update which members
array element to look in when pulling the charges
element. 您需要告知更新在拉动
charges
元素时要查看哪个members
数组元素。 You can do that with the positional $
operator that represents the index of the matched members
array element from the query: 您可以使用位置
$
运算符来完成此操作,该运算符表示查询中匹配的members
数组元素的索引:
db.branches.update(
{'members._id':ObjectId("565dc20d338b60720c25e8d0"),
'members.charges': ObjectId("565dc20d338b60720c25e8d1")
},
{
$pull:{'members.$.charges': ObjectId("565dc20d338b60720c25e8d1") }
}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.