首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MongoDB $pull数组2级

MongoDB $pull数组2级
EN

Stack Overflow用户
提问于 2014-02-07 21:07:32
回答 1查看 981关注 0票数 4

我试图在数组中提取一个元素,其复杂度为e级深

我的文件:

代码语言:javascript
运行
复制
> 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

我试过:

代码语言:javascript
运行
复制
> db.users.update({ mail : 'titi@toto.fr', "commandes.voyagesSouscrits.idVoyage" : "123"},{$pull : {"commandes.$.voyagesSouscrits" : {"commandes.voyagesSouscrits.idVoyage" : "123"}}})

没起作用!

我做错了什么但我找不到它..。知道吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-07 23:51:32

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

代码语言:javascript
运行
复制
db.junk.update(
    { "commandes.voyagesSouscrits.idVoyage": "123" },
    {$pull: { "commandes.$.voyagesSouscrits": { idVoyage: "123" } }}
)

本部分:

代码语言:javascript
运行
复制
idVoyage: { <query> }

之所以需要,是因为"commandes.$.voyagesSouscrits“中的位置运算符只能匹配查询中找到的第一个数组位置。

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

希望这能把它弄清楚。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21637772

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档