MongoDB $ pull数组2级 [英] MongoDB $pull array 2 level

查看:191
本文介绍了MongoDB $ pull数组2级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

只需

是因为"命令"中的位置运算符.$.voyagesSouscrits 中的位置运算符只能与查询中找到的 first 数组位置匹配.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆