在这种情况下预期会有什么行为?mongo 会将空数组视为 null/undefined 并将其包含到稀疏索引中,或者如果数组为空,则不会索引文档?
1 回答
1
空数组的处理方式与nullMongoDB 不同。正如您在 Mongo shell 的以下代码中看到的那样,稀疏索引将空数组查找为空数组,而不是 as null。
> c = db.docs
测试文档
> c.insert({a : []})
> c.ensureIndex({a : 1}, {sparse: true})
> c.find({a : []}).count()
1
> c.find({a : null}).count()
0
与有关 MongoDB 的最有趣的问题一样,使用explain将提供大量信息。例如,您可以看到测试实际上使用了索引,并且索引的边界是null和[],展示了它们的独特处理方式。
> c.find({a : null}).explain()
{
“光标”:“BtreeCursor a_1”,
“isMultiKey”:假,
“n”:0,
“nscannedObjects”:0,
“nscanned”:0,
“nscannedObjectsAllPlans”:0,
“nscannedAllPlans”:0,
“scanAndOrder”:假,
“indexOnly”:假,
“nYields”:0,
“nChunkSkips”:0,
“毫”:0,
“索引边界”:{
“一个” : [
[
无效的,
无效的
]
]
},
“服务器”:“新主机.home:27017”
}
> c.find({a : []}).explain()
{
"cursor" : "BtreeCursor a_1 multi",
“isMultiKey”:假,
“n”:1,
“nscannedObjects”:1,
“nscanned”:1,
“nscannedObjectsAllPlans”:1,
“nscannedAllPlans”:1,
“scanAndOrder”:假,
“indexOnly”:假,
“nYields”:0,
“nChunkSkips”:0,
“毫”:0,
“索引边界”:{
“一个” : [
[
无效的,
无效的
],
[
[ ],
[ ]
]
]
},
“服务器”:“新主机.home:27017”
}
于 2013-04-08T03:18:18.450 回答