Hide
If I create an index:
And then perform query like so:
{ a: 'something', b: null }
|
or
{ a: 'something', b: { $exists: false }
|
My query plan looks like this:
{
|
"queryPlanner" : {
|
...
|
"winningPlan" : {
|
"stage" : "FETCH",
|
"filter" : {
|
"b" : {
|
"$eq" : null
|
}
|
},
|
"inputStage" : {
|
"stage" : "IXSCAN",
|
"keyPattern" : {
|
"a" : 1,
|
"b" : 1
|
},
|
"indexName" : "a_1_b_1",
|
"isMultiKey" : false,
|
"direction" : "forward",
|
"indexBounds" : {
|
"a" : [
|
"['something', 'something']"
|
],
|
"b" : [
|
"[\"null\", \"null\"]"
|
]
|
}
|
}
|
},
|
...
|
},
|
...
|
}
|
This performs badly, I'm assuming because of the filter on the initial fetch. If I remove that by updating all my records with
{ b: null }
, to have
{ b: "null" }
(note the string) and now query for the string null rather than actual null, the plan looks like this.
{
|
"queryPlanner" : {
|
...
|
"winningPlan" : {
|
"stage" : "FETCH",
|
"inputStage" : {
|
"stage" : "IXSCAN",
|
"keyPattern" : {
|
"a" : 1,
|
"b" : 1
|
},
|
"indexName" : "a_1_b_1",
|
"isMultiKey" : false,
|
"direction" : "forward",
|
"indexBounds" : {
|
"a" : [
|
"[\"something\", \"something\"]"
|
],
|
"b" : [
|
"[\"null\", \"null\"]"
|
]
|
}
|
}
|
},
|
...
|
},
|
...
|
}
|
The filter is missing and the query now performs as expected.
Show
If I create an index:
{ a: 1, b: 1 }
And then perform query like so:
{ a: 'something', b: null }
or
{ a: 'something', b: { $exists: false }
My query plan looks like this:
{
"queryPlanner" : {
...
"winningPlan" : {
"stage" : "FETCH",
"filter" : {
"b" : {
"$eq" : null
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"a" : 1,
"b" : 1
},
"indexName" : "a_1_b_1",
"isMultiKey" : false,
"direction" : "forward",
"indexBounds" : {
"a" : [
"['something', 'something']"
],
"b" : [
"[\"null\", \"null\"]"
]
}
}
},
...
},
...
}
This performs badly, I'm assuming because of the filter on the initial fetch. If I remove that by updating all my records with
{ b: null }
, to have
{ b: "null" }
(note the string) and now query for the string null rather than actual null, the plan looks like this.
{
"queryPlanner" : {
...
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"a" : 1,
"b" : 1
},
"indexName" : "a_1_b_1",
"isMultiKey" : false,
"direction" : "forward",
"indexBounds" : {
"a" : [
"[\"something\", \"something\"]"
],
"b" : [
"[\"null\", \"null\"]"
]
}
}
},
...
},
...
}
The filter is missing and the query now performs as expected.