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! :)
Tks für Hilfe Freund, aber hat nicht funktioniert. :( –
Wie funktioniert es nicht für Sie? Ich habe ein paar Verbesserungen daran nur Minuten gemacht. – lascort
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 –