I am having troubles with aggregation pipeline with $sort
The object I got is like this:
{
"fieldA" : a,
"fieldB" : b,
"CreateTime" : timeStamp
}
and I created all three single-field index
then I have the query like this:
db.getCollection('test').aggregate(
[
{
$match:{"fieldA":"a","fieldB":"b"}
},
{
$sort:{"CreateTime":-1}
}
]
)
When I check the explain(), It shows it only hit the “CreateTime” index, which doesn’t really help when the data volume is very large.
However if I try to do the find query like this:
db.getCollection('test').find(
{
"fieldA":"a",
"fieldB":"b"
}
).sort({"CreateTime":-1})
it shows all the indexes are hit and it does help with the performance a lot(finish query within 10 ms with 20 m data volume)
Is there anyway I can make my aggregation use more than one index while there is a “sort” in it?
I know that compound index is a choice, but I’d like to avoid using it since it would be a lot less reusable.
Thanks