Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

spring - MongoDB $pull array 2 level

I'm trying to pull an element in an array with e two level deep complexity

My document :

> db.users.find({ mail : '[email protected]'}).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" : "[email protected]",
        "password" : "tata"
}

In a first step i want to pull the "voyagesSoucrits" elements with id "123".

According to this post : MongoDB pull element from array two levels deep

I tried :

> db.users.update({ mail : '[email protected]', "commandes.voyagesSouscrits.idVoyage" : "123"},{$pull : {"commandes.$.voyagesSouscrits" : {"commandes.voyagesSouscrits.idVoyage" : "123"}}})

It didn't worked !

I did something wrong but i can't find it.... Any idea ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

This part:

idVoyage: { <query> }

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/

Hope that clears it up.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...