devextreme-query-mongodb
devextreme-query-mongodb copied to clipboard
Optimize nested grouping queries
Check https://github.com/oliversturm/devextreme-query-mongodb/blob/master/tests/tests.js#L876
In cases like this, a rather large number of queries is executed. I need the count on each group, but no other details. Can this be optimized, perhaps by using one query with nested groups?
This pipeline queries multiple group levels in one query, using multiple $group stages and starting "from the bottom". Remains to be seen if all other aspects of grouping queries can be integrated in that structure.... there's also the MapReduce querying functionality, if necessary, but perf tests are required to see how much of an improvement that brings.
[{ $match: { int1: {$lt: 5} } }, {"$group": { "_id": {"int1": "$int1", "int2": "$int2"}, "items": { $push: { "string": "$string" }}, "count": { "$sum": 1 }}}, { "$group": { "_id": "$_id.int1", "items": { $push: { key: "$_id.int2", "items": "$items", "count": "$count" } }, count: {"$sum": 1} } } ]