MongoDB Multikeys索引
MongoDB Multikeys索引
MongoDB provides an interesting "multikey" feature that can automatically index arrays of an object's values.但是通过explain,带Multikeys的结果,开始不免有点奇怪.
看下测试例子:
向一个collection中save多条测试语句:
- db.fjx.save({name:"fjx", tags:['weather','hot','record','april']})
- db.fjx.save({name:"fjx1", tags:['weather1','hot','record','april']})
- db.fjx.save({name:"fjx2", tags:['weather2','hot','record','april']})
- db.fjx.save({name:"fjx3", tags:['weather3','hot','record','april']})
- > db.fjx.find({tags:{$regex:"^weather"}}).explain()
- {
- "cursor" : "BtreeCursor tags_1 multi",
- "nscanned" : 4,
- "nscannedObjects" : 4,
- "n" : 4,
- "millis" : 0,
- "nYields" : 0,
- "nChunkSkips" : 0,
- "isMultiKey" : true,
- "indexOnly" : false,
- "indexBounds" : {
- "tags" : [
- [
- "weather",
- "weathes"
- ],
- [
- /^weather/,
- /^weather/
- ]
- ]
- }
- }
- db.fjx.save({name:"fjx4", tags:['weather4','weather5','record','april']})
- > db.fjx.find({tags:{$regex:"^weather"}}).explain()
- {
- "cursor" : "BtreeCursor tags_1 multi",
- "nscanned" : 5,
- "nscannedObjects" : 5,
- "n" : 4,
- "millis" : 0,
- "nYields" : 0,
- "nChunkSkips" : 0,
- "isMultiKey" : true,
- "indexOnly" : false,
- "indexBounds" : {
- "tags" : [
- [
- "weather",
- "weathes"
- ],
- [
- /^weather/,
- /^weather/
- ]
- ]
- }
- }
可以看出nscanned为5条, 而n为4条, 这点大家要注意下, 尤其在给数组建立索引的时候, 因为给数组建立索引它是给数组中的元素建立索引的, 所以它遍历了所以符合索引的元素. 而n为成功搜索的条数.
评论暂时关闭