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"]})
然后给tags数组建立索引!
- > 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/
- ]
- ]
- }
- }
通过查询 以weather开头的explain, 它使用了multi索引. 我们再插入一条.
- 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为成功搜索的条数.Oracle表空间利用率极低时的解决办法Hibernate 主键自动生成的方法相关资讯 MongoDB教程
- MongoDB 入门指南、示例 (07/09/2013 08:38:34)
- VMWare的Cloud Foundry实践(二) (12/10/2012 13:48:40)
- Linux 下 限制 MongoDB 内存占用 (10/22/2012 17:10:01)
| - MongoDB入门必读(概念与实战并重) (07/09/2013 08:30:02)
- MongoDB 可读性比较差的语句 (12/10/2012 13:43:47)
- MongoDB- Java API 增删改操作 (09/15/2012 08:50:26)
|
本文评论 查看全部评论 (0)