Reputation: 720
I have a collection of documents with long values and I want to reduce them into a hierarchical result to reduce bandwidth. Consider the following document as an example:
{
"platform" : "osx",
"buildmode" : "release",
"testsuite" : "geometry sub system",
"testcase" : "comparison of bounding box techniques",
},
{
"platform" : "osx",
"buildmode" : "release",
"testsuite" : "geometry sub system",
"testcase" : "comparison of bounding box techniques",
},
{
"platform" : "win7",
"buildmode" : "debug",
"testsuite" : "payload storage",
"testcase" : "fit-to-size",
}
There are 1200 of these per change revision, and sending back all 1200 is bandwidth heavy, especially since the keywords "osx", "release" and "geometry sub system" are repeated so many times. I would like to return this data so that the top level object has an array of objects with name : 'platform', and buildmodes : [ array ] where the buildmode is an array of objects with 'name' : buildmode and testsuite : [ array ], and on and on. Essentially so that a small number of test suites will contain the test cases within them.
I have done a bit of analysis and have determined this should shrink my output and also make my UI tool (a collection of four combo boxes) easier to write.
Upvotes: 0
Views: 312
Reputation: 75994
You can try below aggregation.
db.collection.aggregate([
{"$group":{
"_id":{"platform":"$platform","buildmode":"$buildmode","testsuite":"$testsuite"},
"testcase":{"$push":"$testcase"}
}},
{"$group":{
"_id":{"platform":"$_id.platform","buildmode":"$_id.buildmode"},
"testsuite":{"$push":{"testsuite":"$_id.testsuite","testcase":"$testcase"}}
}},
{"$group":{
"_id":{"platform":"$_id.platform"},
"buildmode":{"$push":{"buildmode":"$_id.buildmode","testsuite":"$testsuite"}}
}}
])
Upvotes: 2