根据Mongo 文档,
该
{ item : null }
查询匹配包含item
其值为null
或不包含该item
字段的字段的文档。
我找不到这方面的文档,但据我所知,这两种情况(值是null
或字段缺失)都存储在索引中作为null
.
因此,如果我这样做db.orders.createIndex({item: 1})
then db.orders.find({item: null})
,我希望 anIXSCAN
找到所有包含item
其值为null
或不包含该item
字段的字段的文档,并且仅找到那些文档。
那么为什么在db.orders.find({item: null}).explain()
表演完之后还要filter: {item: {$eq: null}}
在FETCH
舞台上表演IXSCAN
呢?可能需要过滤掉哪些可能的文档?
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "temp.orders",
"indexFilterSet" : false,
"parsedQuery" : {
"item" : {
"$eq" : null
}
},
"winningPlan" : {
"stage" : "FETCH",
"filter" : {
"item" : {
"$eq" : null
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"item" : 1
},
"indexName" : "item_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"item" : [
"[null, null]"
]
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "Andys-MacBook-Pro-2.local",
"port" : 27017,
"version" : "3.2.8",
"gitVersion" : "ed70e33130c977bda0024c125b56d159573dbaf0"
},
"ok" : 1
}
我想也许undefined
值会被索引为null
,但简单的实验排除了这一点:
> db.orders.createIndex({item: 1})
{
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.orders.insert({item: undefined})
WriteResult({ "nInserted" : 1 })
> db.orders.find({item: {$type: 6}}).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "temp.orders",
"indexFilterSet" : false,
"parsedQuery" : {
"item" : {
"$type" : 6
}
},
"winningPlan" : {
"stage" : "FETCH",
"filter" : {
"item" : {
"$type" : 6
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"item" : 1
},
"indexName" : "item_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"item" : [
"[undefined, undefined]"
]
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "Andys-MacBook-Pro-2.local",
"port" : 27017,
"version" : "3.2.8",
"gitVersion" : "ed70e33130c977bda0024c125b56d159573dbaf0"
},
"ok" : 1
}