MongoDB Multikeys索引

來源:互聯網
上載者:User

MongoDB provides an interesting "multikey" feature that can automatically index arrays of an object's values. 

但是通過explain,帶Multikeys的結果,開始不免有點奇怪.

看下測試例子:
向一個collection中save多條測試語句:

  1. db.fjx.save({name:"fjx", tags:['weather','hot','record','april']})  
  2. db.fjx.save({name:"fjx1", tags:['weather1','hot','record','april']})  
  3. db.fjx.save({name:"fjx2", tags:['weather2','hot','record','april']})  
  4. db.fjx.save({name:"fjx3", tags:['weather3','hot','record','april']})  
然後給tags數組建立索引!
  1. > db.fjx.find({tags:{$regex:"^weather"}}).explain()  
  2. {  
  3.     "cursor" : "BtreeCursor tags_1 multi",  
  4.     "nscanned" : 4,  
  5.     "nscannedObjects" : 4,  
  6.     "n" : 4,  
  7.     "millis" : 0,  
  8.     "nYields" : 0,  
  9.     "nChunkSkips" : 0,  
  10.     "isMultiKey" : true,  
  11.     "indexOnly" : false,  
  12.     "indexBounds" : {  
  13.         "tags" : [  
  14.             [  
  15.                 "weather",  
  16.                 "weathes"  
  17.             ],  
  18.             [  
  19.                 /^weather/,  
  20.                 /^weather/  
  21.             ]  
  22.         ]  
  23.     }  
  24. }  
通過查詢 以weather開頭的explain, 它使用了multi索引. 我們再插入一條.
  1. db.fjx.save({name:"fjx4", tags:['weather4','weather5','record','april']})  
  2. > db.fjx.find({tags:{$regex:"^weather"}}).explain()  
  3. {  
  4.     "cursor" : "BtreeCursor tags_1 multi",  
  5.     "nscanned" : 5,  
  6.     "nscannedObjects" : 5,  
  7.     "n" : 4,  
  8.     "millis" : 0,  
  9.     "nYields" : 0,  
  10.     "nChunkSkips" : 0,  
  11.     "isMultiKey" : true,  
  12.     "indexOnly" : false,  
  13.     "indexBounds" : {  
  14.         "tags" : [  
  15.             [  
  16.                 "weather",  
  17.                 "weathes"  
  18.             ],  
  19.             [  
  20.                 /^weather/,  
  21.                 /^weather/  
  22.             ]  
  23.         ]  
  24.     }  
  25. }  

可以看出nscanned為5條, 而n為4條, 這點大家要注意下, 尤其在給數組建立索引的時候, 因為給數組建立索引它是給數組中的元素建立索引的, 所以它遍曆了所以符合索引的元素. 而n為成功搜尋的條數.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.