1. 程式人生 > >MongoDB 學習筆記之 $or與索引關系

MongoDB 學習筆記之 $or與索引關系

其中 沒有 默認 ont mes exp style explain alt

$or與索引關系:

技術分享

對leftT集合的timestamp創建索引

技術分享

執行$or語句:db.leftT.find({$or: [{ "timestamp" : 5},{"age": 10}]}).explain(true),發現沒有使用任何索引

技術分享

對age再創建索引

技術分享

重新再次查看查詢計劃:

技術分享

索引被使用了。

結論:

如果使用了$or操作符,必須保證關鍵字都有索引,如果其中任何一個關鍵字沒有索引,則默認沒有索引,會使用全表掃描。

MongoDB 學習筆記之 $or與索引關系