MongoDB和覆盖数组的索引可能吗?

时间:2011-08-18 16:48:02

标签: arrays search mongodb indexing

说我添加了一份文件

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:我认为对于后者正则表达式会造成一些麻烦......至少这是我的猜测。

感谢您的反馈。

干杯

1 个答案:

答案 0 :(得分:2)

目前无法在数组上使用覆盖索引。这是因为数组中的每个元素都是单独索引的,因此索引中没有包含整个数组的条目。