Mongo aggregate not using index
My mongo search query uses an index, but the same functionality, if I implement using an aggregate, it doesn't use an index.
db.collection1.find({Attribute8: "s1000",Attribute9: "s1000"}).sort({Attribute10: 1})
"cursor used in search": "BtreeCursor Attribute8_1_Attribute9_1_Attribute10_1"
db.collection1.aggregate([
{
$match: {
Attribute8: "s1000",
Attribute9: "s1000"
}
},
{
$sort: {
Attribute10: 1
}
}
])
"cursor used in combination": "BtreeCursor".
Can someone tell me where this went wrong. My goal is to use indexes in an aggregated method. Thanks in advance.
source to share
After some digging, the problem comes with limiting the use of the following types:
Symbol, MinKey, MaxKey, DBRef, Code and CodeWScope
In this case, Symbol is used to store a string value, so the index doesn't work.
Try with en set to explain true in the aggregate option.
[EDIT] My previous answer is wrong.
The aggregation pipeline uses "BtreeCursor" (only when the specified field has an index) to run the $ match query and uses the checked index, checks the "indexBound" to check.
Ensuring that the entire collection is indexed in "Attribute08"
db.temps.ensureIndex ({Attribute08: 1})
$ match on the field with index:
db.temps.aggregate ([{$ match: {Attribute08: "s1000"}}], {explain: true}) "allPlans": [ { "cursor": "BtreeCursor", "isMultiKey": false, "scanAndOrder": false, "indexBounds": { "Attribute08": [ [ "s1000", "s1000" ] ] } } ]
Below $ match in a field with no index:
db.temps.aggregate ([{$ match: {Attribute09: "s1000"}}], {explain: true}) "allPlans": [ { "cursor": "BasicCursor", "isMultiKey": false, "scanAndOrder": false } ]
source to share