MongoDB
 sql >> Baza danych >  >> NoSQL >> MongoDB

Jak połączyć i posortować kilka kolekcji w jednym potoku w Mongo

Najnowsza wersja Mongo 3.4, będziesz korzystać z $ aspekt do przetwarzania tych samych danych w wielu potoku agregacji i łączenia wyników z całej agregacji z jednym wyjściem.

db.users.aggregate([
    { "$match": { "_id":1} },
    {
        "$facet": {
            "Received": [
                 {$lookup: {from: 'requests', localField: "_id", foreignField: "userId", as: "receivedRequest"}},
                 {$unwind: '$receivedRequest'},
                 {$lookup: {from: 'users', localField: "receivedRequest.requesterId", foreignField: "_id", as: "receivedUser"}},
                 {$project: {_id: '$receivedRequest.requesterId', profile: '$receivedUser.profile', weight: {$add: [4]}}}
            ],
            "Sent": [
                 {$lookup: {from: 'requests', localField: "_id", foreignField: "requesterId", as: "sentRequest"}},
                 {$unwind: '$sentRequest'},
                 {$lookup: {from: 'users', localField: "sentRequest.userId", foreignField: "_id", as: "sendUser"}},
                 {$project: {_id: '$sentRequest.userId', profile: '$sendUser.profile', weight: {$add: [3]}}}
            ],
            "Friends": [
                 {$lookup: {from: 'friends', localField: "_id", foreignField: "userId", as: "friends"}},
                 {$unwind: '$friends'},
                 {$lookup: {from: 'users', localField: "friends.friendId", foreignField: "_id", as: "friendUser"}},
                 {$project: {_id: '$friends.friendId', profile: '$friendUser.profile', weight: {$add: [2]}}}
            ],
            "Others": [
                {$lookup: {from: 'friends', localField: "_id", foreignField: "friendId", as: "others"}},
                 {$unwind: '$others'},
                 {$lookup: {from: 'users', localField: "others.userId", foreignField: "_id", as: "other"}},
                 {$project: {_id: '$others.userId', profile: '$other.profile', weight: {$add: [1]}}}
            ]
        }
    }
]).pretty()

Przykładowe wyjście:

{
        "Received" : [
                {
                        "_id" : 3,
                        "profile" : [
                                {
                                        "name" : "John"
                                }
                        ],
                        "weight" : 4
                }
        ],
        "Sent" : [
                {
                        "_id" : 4,
                        "profile" : [
                                {
                                        "name" : "Jessica"
                                }
                        ],
                        "weight" : 3
                }
        ],
        "Friends" : [
                {
                        "_id" : 2,
                        "profile" : [
                                {
                                        "name" : "Ana"
                                }
                        ],
                        "weight" : 2
                }
        ],
        "Others" : [
                {
                        "_id" : 5,
                        "profile" : [
                                {
                                        "name" : "Sheldon"
                                }
                        ],
                        "weight" : 1
                }
        ]
}



  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. Zapytanie z mangustą i daktylami

  2. MongoDB:Jak rozwiązać DBRef po stronie klienta?

  3. Jak stworzyć usługę CUPS dla mongoDB?

  4. Najszybszy sposób na usunięcie duplikatów dokumentów w mongodb

  5. Wyniki zapytania projekcji Mongo różnią się od testowania w aplikacji shell vs nodejs