我正试图在一个具有两级深度复杂性的数组中拉出一个元素
我的文件:
> 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”元素。
根据这篇文章: MongoDB pull element from array two levels deep
我试过了:
> db.users.update({ mail : 'titi@toto.fr', "commandes.voyagesSouscrits.idVoyage" : "123"},{$pull : {"commandes.$.voyagesSouscrits" : {"commandes.voyagesSouscrits.idVoyage" : "123"}}})
没用了!
我做错了什么但我找不到它......任何想法?
答案 0 :(得分:1)
您不需要完整的表示法,因为占位符已经移动到数组中的该位置。
db.junk.update(
{ "commandes.voyagesSouscrits.idVoyage": "123" },
{$pull: { "commandes.$.voyagesSouscrits": { idVoyage: "123" } }}
)
这部分:
idVoyage: { <query> }
只需要,因为“ commandes。$。voyagesSouscrits ”中的位置操作符只能匹配查询中找到的第一个数组位置。
http://docs.mongodb.org/manual/reference/operator/projection/positional/
希望能够解决它。