mongodb Aggregat-Dokument eingebettete Werte

Im Strugling mit einigen aggregation-Funktionen in mongodb.

Sagen, ich habe einige Dokumente wie dieses

 [
 {
    _id: "1",
    periods: [
      {
         _id: "12",
         tables: [
           {
              _id: "121",
              rows: [
                  { _id: "1211", text: "some text"},
                  { _id: "1212", text: "some other text"},
                  { _id: "1213", text: "yet another text"},

              ]
           }
         ]
      },
      {
         _id: "13",
         tables: [
           {
              _id: "131",
              rows: [
                  { _id: "1311", text: "different text"},
                  { _id: "1312", text: "Oh yeah"}                      
              ]
           }
         ]
      }
    ]
 },
 {
    _id: "2",
    periods: [
      {
         _id: "21",
         tables: [
           {
              _id: "212",
              rows: [
                  { _id: "2121", text: "period2 text"},
                  { _id: "2122", text: "period2 other text"},
                  { _id: "2123", text: "period2 yet another text"},

              ]
           }
         ]
      }
    ]
 }
 ]

Jetzt will ich mit einem mongodb-Abfrage zum abrufen aller einzigartige Texte für eine bestimmte top-level-Element.

zB agregate alle Texte für die top _id 1. Dies würde bedeuten, dass ich möchte, um all die Texte, die sowohl in der Periode von teilstrukturen.

erwartete Ausgabe würde wie folgt Aussehen:

Aggregat Texte filtern, die auf _id: 1

[
   "some text",
   "some other text",
   "yet another text",
   "different text",
   "Oh yeah"
]

Aggregat Texte filtern, die auf _id: 2

[
  "period2 some text",
  "period2 some other text",
  "period2 yet another text"
]

So weit ich habe es geschafft, sammeln Sie alle die Texte , aber die am Ende in mehrere arrays und ich habe es nicht geschafft, die filter auf die id mit $match,

Meine aktuellen aggregate-query sieht wie folgt aus

[ 
    { "$project" : { "text" : "$periods.tables.rows.text" , "_id" : "$_id"}},
    { "$unwind" : "$text"},
    { "$group" : { "_id" : "$_id" , "texts" : { "$addToSet" : "$text"}}},
    { "$project" : { "_id" : 0 , "texts" : 1}} 
]

Es gibt mir ein Ergebnis loooking so etwas

{ "texts" : [ 
        [ [ "Some text" , "Some other text" , "yet another text"] , [ "different text" , "oh yeah" ] ],
        [ [ "period2 some text", "period2 some other text", "period2 yet another text"]]
    ]}

Wenn ich auf add $match: {_id: 1}, werden keine Ergebnisse zurückgegeben.

Kann jemand bitte helfen Sie mir mit diesem, oder zeigen Sie mich in eine Richtung auf, wie es zu lösen. Ich habe die Suche nach Ressourcen, aber nicht scheinen zu finden, eine gute Dokumentation, wie man diese Aggregat-Funktionen. Die mongodb docs verwenden Sie nur einfache Dokumente.

PS ich weiß, ich kann dies tun, indem Sie mapreduce, wurde aber in der Hoffnung, in der Lage zu verwenden ein Aggregat-Funktion für diese.

InformationsquelleAutor Tommy | 2013-09-13
Schreibe einen Kommentar