Answer the question
In order to leave comments, you need to log in
How to speed up aggregation in MongoDB 3.2?
I have a query like:
db.foo.aggregate([{$group: {_id: 1, 'myavg': {$avg:'$value'}}}])
Changed in version 3.2: Starting in MongoDB 3.2, indexes can cover an aggregation pipeline. In MongoDB 2.6 and 3.0, indexes could not cover an aggregation pipeline since even when the pipeline uses an index, aggregation still requires access to the actual documents.
{
"waitedMS" : NumberLong(0),
"stages" : [
{
"$cursor" : {
"query" : {
},
"fields" : {
"correct" : 1,
"_id" : 0
},
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "mydb.foo",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [ ]
},
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"$and" : [ ]
},
"direction" : "forward"
},
"rejectedPlans" : [ ]
}
}
},
{
"$group" : {
"_id" : {
"$const" : 1
},
"myavg" : {
"$avg" : "$correct"
}
}
}
],
"ok" : 1
}
Answer the question
In order to leave comments, you need to log in
The database has a million records. Works within 2-3 seconds. This is too much, I would like it faster.For this query, indexes will not help much, because all documents are processed (without filtering).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question