Spring MongoDB$pull阵列2级

Spring MongoDB$pull阵列2级,spring,mongodb,mongodb-query,spring-data-mongodb,mongodb-update,Spring,Mongodb,Mongodb Query,Spring Data Mongodb,Mongodb Update,我试图从一个数组中抽取一个元素,这个数组有两级复杂度 我的文件: > db.users.find({ mail : 'titi@toto.fr'}).pretty() { "_class" : "bean.User", "_id" : ObjectId("52f504bb2f9dd91186211537"), "commandes" : [ { "adresse"

我试图从一个数组中抽取一个元素,这个数组有两级复杂度

我的文件:

> 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”的“VoyageSoucrits”元素

根据这篇文章:

我试过:

> db.users.update({ mail : 'titi@toto.fr', "commandes.voyagesSouscrits.idVoyage" : "123"},{$pull : {"commandes.$.voyagesSouscrits" : {"commandes.voyagesSouscrits.idVoyage" : "123"}}})
没用


我做错了什么,但我找不到。。。。有什么想法吗?

您不需要完整的符号,因为占位符已移动到数组中的该位置

db.junk.update(
    { "commandes.voyagesSouscrits.idVoyage": "123" },
    {$pull: { "commandes.$.voyagesSouscrits": { idVoyage: "123" } }}
)
本部分:

idVoyage: { <query> }
idVoyage:{}
之所以需要,是因为“命令。$.VoyageSousCrits”中的位置运算符只能匹配查询中找到的第一个数组位置


希望这能解决问题。

如果我想在升级属性上的$pull上添加约束?如下所示:{$pull:{“commandes.$.voyagesSouscrits”:{dateCommande:null,“idVoyage”:“123”}}}}如果它不在commandes.$.voyagesSouscrits级别的数组中,则这不起作用。对于要更改查询部分的任何其他约束。