Zum Beispiel habe ich eine Sammlung:MongoDB Gruppe mit Art und Grenze für jede Gruppe
{ "_id" : 1, "name" : "abc", "score" : 10 }
{ "_id" : 2, "name" : "abc", "score" : 15 }
{ "_id" : 3, "name" : "abc", "score" : 20 }
{ "_id" : 4, "name" : "xyz", "score" : 10 }
{ "_id" : 5, "name" : "xyz", "score" : 15 }
{ "_id" : 6, "name" : "xyz", "score" : 20 }
Wie kann ich eine Abfrage in MongoDB zu einer Gruppe tun, indem name
dann sortiert nach score
und nehmen Sie es mit limit=2
. Ich möchte wie folgt erhalten:
{"_id": "abc", "items": [
{ "_id" : 3, "name" : "abc", "score" : 20 },
{ "_id" : 2, "name" : "abc", "score" : 15 }]
}
{"_id": "xyz", "items": [
{ "_id" : 6, "name" : "xyz", "score" : 20 },
{ "_id" : 5, "name" : "xyz", "score" : 15 }]
}
Haben Sie das versucht? –