복붙노트

[SPRING] MongoDB $ 배열 2 레벨 당김

SPRING

MongoDB $ 배열 2 레벨 당김

나는 두 가지 레벨의 깊이있는 복잡성을 가진 배열에서 요소를 가져 오려고 노력하고있다.

내 문서 :

> 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는 배열에서 2 레벨 깊이의 요소를 가져옵니다.

나는 시도했다 :

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

그것은 효과가 없었어요!

나는 틀린 일을했지만, 나는 그것을 발견 할 수 없다 .... 어떤 생각?

해결법

  1. ==============================

    1.자리 표시자가 배열의 해당 위치로 이미 이동 했으므로 전체 표기가 필요하지 않습니다.

    자리 표시자가 배열의 해당 위치로 이미 이동 했으므로 전체 표기가 필요하지 않습니다.

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

    이 부분:

    idVoyage: { <query> }
    

    "명령. $ .voyagesSouscrits"의 위치 연산자가 쿼리에서 찾은 첫 번째 배열 위치와 만 일치 할 수 있기 때문에 필요합니다.

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

    그것을 버리는 희망.

  2. from https://stackoverflow.com/questions/21637772/mongodb-pull-array-2-level by cc-by-sa and MIT license