2011-08-18 74 views
2

說我加入MongoDB和覆蓋數組上的索引可能嗎?

db.search.save({terms:["10115","Berlin","Germany"]}) 

我通過添加一個索引文件:

db.search.ensureIndex({terms:1}) 

在搜索文件時,並確保只有索引被取出

db.search.find({terms:"Berlin"}, {terms:1,_id:0}).explain() 

# note: causes the whole output of the array, which might cause some trouble? 
# however, leaving it out doesn't change anything 

我會預計在.explain()內

"indexOnly" : true 

遊標指示mongo正在使用索引(BtreeCursor terms_1)。 所以我想知道如果mongo能夠通過只觸摸索引輸出數組索引。

最後我想要做一個查詢,如:

db.search.find({terms:{$all:[/10115/i,/ger/i]}}) 

PS:我看到,後者正則表達式可能會引起一些麻煩......至少那是我的猜測。

感謝您的反饋。

乾杯

回答

2

目前不能在數組上使用覆蓋索引。這是因爲數組中的每個元素都是分別索引的,所以索引中沒有包含整個數組的索引。

+0

我有這個想法(見我的筆記)。我也做了一些測試,似乎沒有獲取術語字段沒有任何區別...除非有任何魔術技巧:) – fightbulc