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.
(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:
cannot use the part (members of members.charges) to traverse the element.
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. You can do that with the positional $
operator that represents the index of the matched members
array element from the query:
db.branches.update(
{'members._id':ObjectId("565dc20d338b60720c25e8d0"),
'members.charges': ObjectId("565dc20d338b60720c25e8d1")
},
{
$pull:{'members.$.charges': ObjectId("565dc20d338b60720c25e8d1") }
}
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.