【问题标题】:MongoDB $pull array 2 levelMongoDB $pull 数组 2 级
【发布时间】:2014-03-05 10:57:25
【问题描述】:

我正在尝试在具有 e 两级深度复杂性的数组中提取一个元素

我的文件:

> 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"}}})

没用!

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

【问题讨论】:

    标签: spring mongodb mongodb-query spring-data-mongodb mongodb-update


    【解决方案1】:

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

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

    这部分:

    idVoyage: { <query> }
    

    是唯一需要的,因为“commandes.$.voyagesSouscrits”中的位置运算符只能匹配查询中找到的first数组位置。

    http://docs.mongodb.org/manual/reference/operator/projection/positional/

    希望能解决这个问题。

    【讨论】:

    • 如果我想在升级属性上对我的 $pull 添加一个约束?像这样: {$pull : {"commandes.$.voyagesSouscrits" : {dateCommande : null, "idVoyage" : "123"}}}
    • 如果它不在 commandes.$.voyagesSouscrits 级别的数组内,那么这不起作用。对于您想要更改查询部分的任何其他约束。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    • 2021-01-22
    • 2013-02-13
    • 1970-01-01
    • 2019-02-10
    • 2014-06-01
    相关资源
    最近更新 更多