MongoDB $pull 数组 2 级 [英] MongoDB $pull array 2 level
问题描述
我正在尝试从具有两级深度复杂度的数组中提取一个元素
I'm trying to pull an element in an array with e two level deep complexity
我的文档:
> db.users.find({ mail : 'titi@toto.fr'}).pretty()
{
"_class" : "bean.User",
"_id" : ObjectId("52f504bb2f9dd91186211537"),
"commandes" : [
{
"adresse" : "15 rue de la soif",
"codePostal" : "29200",
"idCommande" : 1,
"montantTotal" : 0,
"nom" : "TOTO",
"prenom" : "tata",
"ville" : "Brest",
"voyagesSouscrits" : [
{
"idVoyage" : "123",
"duree" : 1,
"nbPersonnes" : 0,
"villeDepart" : "Nantes",
"prixVoyage" : 999999
},
{
"idVoyage" : "addVoyageSouscrit",
"duree" : 1,
"nbPersonnes" : 0,
"villeDepart" : "Toulouse",
"prixVoyage" : 7777
}
]
},
{
"idCommande" : 1,
"dateCommande" : ISODate("2014-02-07T16:07:23.930Z"
),
"nom" : "TOTO",
"prenom" : "tata",
"adresse" : "15 rue de la soif",
"ville" : "Brest",
"codePostal" : "29200",
"montantTotal" : 0,
"voyagesSouscrits" : [
{
"idVoyage" : "123",
"duree" : 1,
"nbPersonnes" : 0,
"villeDepart" : "Toulouse",
"prixVoyage" : 666666
}
]
}
],
"mail" : "titi@toto.fr",
"password" : "tata"
}
第一步,我想拉取 ID 为123"的voyagesSoucrits"元素.
In a first step i want to pull the "voyagesSoucrits" elements with id "123".
根据这篇文章:MongoDB 从两层深的数组中拉取元素
我试过了:
> db.users.update({ mail : 'titi@toto.fr', "commandes.voyagesSouscrits.idVoyage" : "123"},{$pull : {"commandes.$.voyagesSouscrits" : {"commandes.voyagesSouscrits.idVoyage" : "123"}}})
它没有用!
我做错了什么,但我找不到......知道吗?
I did something wrong but i can't find it.... Any idea ?
推荐答案
您不需要完整的表示法,因为占位符已经移动到数组中的那个位置.
You don't need the full notation as the placeholder has already moved to that position in the array.
db.junk.update(
{ "commandes.voyagesSouscrits.idVoyage": "123" },
{$pull: { "commandes.$.voyagesSouscrits": { idVoyage: "123" } }}
)
这部分:
idVoyage: { <query> }
仅需要,因为commandes.$.voyagesSouscrits"中的位置运算符只能匹配查询中找到的第一个数组位置.
is only needed because the positional operator in "commandes.$.voyagesSouscrits" can only match the first array position found in the query.
http://docs.mongodb.org/manual/reference/operator/projection/positional/
希望能解决问题.
这篇关于MongoDB $pull 数组 2 级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!