2017-07-11 3 views
0

Ich muss die gesamte Einzelobjekthierarchie aus der Datenbank als JSON abrufen. Ich versuche Aggregate für Stunden und kann nicht lösen, wie es mit meinen Daten geht. So habe ich drei Kollektionen:MongoDB verschachtelte Suche mit 3 untergeordneten Ebenen

Form

{ "_id" : "1", "name" : "My first form" } 
{ "_id" : "2", "name" : "Second one" } 
{ "_id" : "3", "name" : "Another" } 

Frage

{ "_id" : "q1", "form" : "1", "title": "What's your country?"} 
{ "_id" : "q2", "form" : "1", "title": "What your favorite color?"} 
{ "_id" : "q3", "form" : "1", "title": "Where do you live?"} 
{ "_id" : "q4", "form" : "2", "title": "Where to go?"} 

Option

{ "_id" : "o1", "question" : "q1", "text" : "Brazil" } 
{ "_id" : "o2", "question" : "q1", "text" : "EUA" } 
{ "_id" : "o3", "question" : "q1", "text" : "China" } 
{ "_id" : "o4", "question" : "q2", "text" : "Red" } 
{ "_id" : "o5", "question" : "q2", "text" : "Blue" } 
{ "_id" : "o6", "question" : "q2", "text" : "Green" } 

Ich muss wieder trieve jedes Formular mit allen entsprechenden Fragen, und in jeder Frage die Optionen dafür. Wie folgt aus:

[ 
    { 
     _id:"q1", 
     name: "My first form", 
     questions: [ 
      { "_id" : "q1", 
      "form" : "1", 
      "title": "What's your country?", 
      "options": [ 
        { "_id" : "o1", "question" : "q1", "text" : "Brazil" } 
        { "_id" : "o2", "question" : "q1", "text" : "EUA" }, 
        { "_id" : "o3", "question" : "q1", "text" : "China" } 
      ] 
      }, 
      { "_id" : "q2", 
      "form" : "1", 
      "title": "What your favorite color", 
      "options": [ 
        { "_id" : "o4", "question" : "q2", "text" : "Red" } 
        { "_id" : "o5", "question" : "q2", "text" : "Blue" }, 
        { "_id" : "o6", "question" : "q2", "text" : "Green" } 
      ] 
      }, 
      { "_id" : "q3", 
      "form" : "1", 
      "title": "Where do you live?", 
      "options": [] 
      } 
     ] 
    }, 
    ... 
] 

Ich habe eine Menge von $ Lookup versucht, $ Abroller, weitere $ Lookup und $ Projekt, aber nichts geben-mir das Ergebnis (Formulare mit Fragen nach innen, Fragen mit Optionen innen).

Bitte, hilf mir! :)

Antwort

1

Ich denke, die rund um die question Sammlung abfragt, Nachschlagen ihre question s und Gruppierung von form und schließlich Nachschlag Formular und das Projekt, in dieser Reihenfolge.

Dies sollte es tun. Beachten Sie, dass _id in der Ausgabe dieses Aggregats das Formular _id ist.

db.question.aggregate([ 
    {$match: {}}, 
    {$lookup: { 
     from: 'option', 
     localField: '_id', 
     foreignField: 'question', 
     as: 'options' 
    }}, 
    {$group: { 
     _id: "$form", 
     questions: {$push: { 
      title: "$title", 
      options: "$options", 
      form: "$form" 
     }} 
    }}, 
    {$lookup: { 
     from: 'form', 
     localField: "_id", 
     foreignField: "_id", 
     as: 'form' 
    }}, 
    {$project: { 
     name: {$arrayElemAt: ["$form.name", 0]}, 
     questions: true 
    }} 
]); 

Eigentlich .. das scheint eine bessere Alternative. Es wird form s ohne question s auch zurückgeben.

db.form.aggregate([ 
    {$match: {}}, 
    {$lookup: { 
     from: 'question', 
     localField: '_id', 
     foreignField: 'form', 
     as: 'questions' 
    }}, 
    {$unwind: { 
     path: "$questions", 
     preserveNullAndEmptyArrays: true 
    }}, 
    {$lookup: { 
     from: 'option', 
     localField: 'questions._id', 
     foreignField: 'question', 
     as: 'options' 
    }}, 
    {$group: { 
     _id: "$_id", 
     name: {$first: "$name"}, 
     question: {$push: { 
      title: "$questions.title", 
      form: "$questions.form", 
      options: "$options" 
     }} 
    }} 
]) 
+0

Tks für Hilfe Freund, aber hat nicht funktioniert. :( –

+0

Wie funktioniert es nicht für Sie? Ich habe ein paar Verbesserungen daran nur Minuten gemacht. – lascort

+0

Ich habe Ihre letzte Bearbeitung. Ich sah es. Die Struktur von Ihnen ergibt, sind richtig. Aber es hat keine Daten. Werfen Sie einen Blick auf die Ergebnisse hier: https://pastebin.com/qddteCTq –

Verwandte Themen