Hello guys,
I have the query "db.col.find({field1: "val1", field2: "val2"})" and a field that covers both "field1" and "field2".
I have a new requirement which is to iterate over the resultset pages using a cursor pattern based on the collectioon "_id" field.
So, i need now to add a new field filter and sort the query: "db.col.find({field1: "val1", field2: "val2", _id: {$gt: "val3"} }).sort("_id").limit(100)".
I have ran both queries and see the execution plan results.
What I see is that the new query do uses the index and adds also a FETCH/filter stage.
So my question is if I do really have to add a new index to the collection covering also the _id field? better if I don't
First query with no "_id" filter and no sort shows:
...
"winningPlan" : {
"stage" : "LIMIT",
"limitAmount" : 0,
"inputStage" : {
"stage" : "SHARDING_FILTER",
"inputStage" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
...
Second query with _id filter and sort shows:
...
"winningPlan" : {
"stage" : "LIMIT",
"limitAmount" : 0,
"inputStage" : {
"stage" : "SHARDING_FILTER",
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"_id" : {
"$gt" : "1234"
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
...
See the new filter section on the last plan above the IXSCAN section.
Is this still a good plan? or should I need to add a new index?
(the original filter throws no more than 100k documents)