2017-06-09 5 views
1

Ich habe einige Probleme bekommen, die Ausgabe, die ich will. Im Folgenden finden Sie die Beispieldatenstruktur aus meiner Sammlung „testdata_4“ genannt:Aggregieren, um verschachtelte Listen auszugeben

db.testdata_4.aggregate([{ 
    "$unwind": "$Location" 
},{ 
    "$group": { 
     "_id": { 
      "Locations": "$Location", 
      "Year": "$Date" 
     }, 
     Links: { 
      $addToSet: "$Link" 
     }, 
     Titles: { 
      $addToSet: "$Title" 
     } 
    } 
}, { 
    "$sort": { "_id.Year": 1 } 
},{ 
    "$group": { 
     "_id": "$_id.Locations", 
     Records: { 
      $push: { 
       "Year": "$_id.Year", 
       "Links": { $setUnion: ["$Links", "$Titles"]} 
      } 
     } 
    } 
},{ 
    "$sort": { "_id": 1 } 
}]).toArray() 

Und die Ausgabe, die ich von dem bekommen habe:

[ 
    { 
     "_id": 1, 
     "Record": 1, 
     "Link": "www.google.com", 
     "Link_Title": "Google", 
     "Location": ["loc1", "loc2", "loc3", "loc4"], 
     "Date": 2017, 
     "People": ["ppl1", "ppl2", "ppl3", "ppl4"] 
    }, 
    { 
     "_id": 2, 
     "Record": 2, 
     "Link": "www.facebook.com", 
     "Link_Title": "Facebook", 
     "Location": ["loc1", "loc2", "loc3", "loc4"], 
     "Date": 2016, 
     "People": ["ppl1", "ppl2", "ppl3", "ppl4"] 
    } 
] 

Die Abfrage, die ich versucht habe, diese zu bedienen sind Abfrage oben ist dies:

[ 
    { 
     "_id" : "loc2", 
     "Records" : [ 
      { 
       "Year" : 2016, 
       "Links" : [ 
        "CooCoo", 
        "Facebook", 
        "Google", 
        "www.coocoo.com", 
        "www.facebook.com", 
        "www.google.com" 
       ] 
      } 
     ] 
    }, 
    { 
     "_id" : "loc3", 
     "Records" : [ 
      { 
       "Year" : 2017, 
       "Links" : [ 
        "CooCoo", 
        "Facebook", 
        "www.coocoo.com", 
        "www.facebook.com" 
       ] 
      } 
     ] 
    } 
] 

jedoch die Ausgabe, die ich oben bekommen habe, ist ein bisschen etwas weg von der Ausgabe, die ich bekommen möchten, die unten wie die Beispielausgabe aussehen soll (wieder mit dem Ausgang oben):

Also meine Frage ist, ist es für mich möglich zu aggregieren und die Ausgabe zu erhalten, wie ich oben will, oder überhaupt nicht möglich? Wenn es möglich ist, wird jede gelieferte Lösung sehr willkommen sein, solange es mir hilft, ein wenig voranzukommen! Danke im Voraus!

Antwort

1

auch Diese Abfrage kann geben Ihnen

db.testdata_4.aggregate([ 
    {"$unwind": "$Location"}, 
    {"$group": { 
    _id: {"Locations": "$Location","Year": "$Date"}, 
    Links: { $addToSet: {Link: "$Link", Title: "$Link_Title"}} 
    }}, 
    {"$sort": {"_id.Year": 1}}, 
    {"$group": { 
    "_id": "$_id.Locations", 
    Records: {$push: {"Year": "$_id.Year", "Links": "$Links"}} 
    }}, 
    {"$sort": {"_id": 1}} 
]).toArray() 
Ausgang erwartet
2

Wenn ich Ihre Absicht richtig lesen, dann Sie Gruppe auf alles für verschiedene Werte statt $addToSet:

db.testdata_4.aggregate([ 
    { "$unwind": "$Location" }, 
    { "$group": { 
    "_id": { 
     "Location": "$Location", 
     "Year": "$Date", 
     "Title": "$Link_Title", 
     "Link": "$Link" 
    } 
    }}, 
    { "$group": { 
    "_id": { 
     "Location": "$_id.Location", 
     "Year": "$_id.Year", 
    }, 
    "Links": { "$push": { 
     "Title": "$_id.Title", 
     "Link": "$_id.Link" 
    }} 
    }}, 
    { "$sort": { "_id.Year": 1 } }, 
    { "$group": { 
    "_id": "$_id.Location", 
    "Records": { 
     "$push": { 
     "Year": "$_id.Year", 
     "Links": "$Links" 
     } 
    } 
    }} 
]) 

So, nachdem Sie $unwind das Array Sie alles, was in den _id Schlüssel $group stellen die unterschiedlichen Werte zu erhalten .

Dann ist es nur eine Frage der Gruppierung zuerst nach Ort und Jahr und Erstellen der "Links" -Array, dann wieder gruppieren nur an der Stelle, um die "Records" -Array zu erstellen.

Verwandte Themen