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

MongoDB Pokaż przedmioty dla dzieci w relacji jeden do wielu

Poniższy potok powinien dać ci pomysł

db.getCollection('yourCollection').aggregate(
    {
        $unwind: {
            path: "$dates",
            includeArrayIndex: "idx"
        }
    },
    {
        $project: {
            _id: 0,
            dates: 1,
            numbers: { $arrayElemAt: ["$numbers", "$idx"] },
            goals: { $arrayElemAt: ["$goals", "$idx"] },
            durations: { $arrayElemAt: ["$durations", "$idx"] }
        }
    }
)

co daje następujące dane wyjściowe dla twojej próbki

/* 1 */
{
    "dates" : NumberLong(1399518702000),
    "numbers" : "5982",
    "goals" : "1",
    "durations" : "78"
}

/* 2 */
{
    "dates" : NumberLong(1399126333000),
    "numbers" : "5983",
    "goals" : "0",
    "durations" : "45"
}

/* 3 */
{
    "dates" : NumberLong(1399209192000),
    "numbers" : "5984",
    "goals" : "4",
    "durations" : "90"
}

/* 4 */
{
    "dates" : NumberLong(1399027545000),
    "numbers" : "5985",
    "goals" : "2",
    "durations" : "90"
}



  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. Warunek zapytania MongoDb przy porównaniu 2 pól

  2. MongoDB na Ubuntu nie uruchamia się jako usługa, nic w dzienniku

  3. mongod HostnameCanonicalizationWorker błąd w systemie OS X

  4. Odpytywanie MongoDB o dopasowanie w pierwszym elemencie tablicy

  5. WiredTiger i aktualizacje na miejscu