Answer the question
In order to leave comments, you need to log in
How to do grouping in mongodb?
There is a collection with documents of the form:
{ "_id" : ObjectId("56e17f2db292c9151a8b459b"), "title" : "test title", "category" : "test category", "pubDate" : "1457599100", "code" : "ZZZ" },
{ "_id" : ObjectId("56e17f2db292c9151a8b459b"), "title" : "test 2 title", "category" : "test 2 category", "pubDate" : "1457599200", "code" : "ZZZ" }
{
"ZZZ": {
"items": {
0: { "_id" : ObjectId("56e17f2db292c9151a8b459b"), "title" : "test title", "category" : "test category", "pubDate" : "1457599100"},
1: { "_id" : ObjectId("56e17f2db292c9151a8b459b"), "title" : "test 2 title", "category" : "test 2 category", "pubDate" : "1457599100"}
}
}
}
Answer the question
In order to leave comments, you need to log in
Answered on stackoverflow:
db.collection.aggregate([{ $group : { _id : "$code", items: { $push: "$$ROOT" } } }])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question