【问题标题】:MongoDB and covering index on arrays possible?MongoDB和数组上的覆盖索引可能吗?
【发布时间】:2011-08-18 16:48:01
【问题描述】:

假设我添加了一个文档

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:我发现对于后者,Regex 可能会造成一些麻烦……至少这是我的猜测。

感谢您的反馈。

干杯

【问题讨论】:

    标签: arrays search mongodb indexing


    【解决方案1】:

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

    【讨论】:

    • 我有这个想法(见我的笔记)。我也做了一些测试,似乎不获取术语字段没有任何区别......除非有任何魔术:)
    猜你喜欢
    • 2015-10-24
    • 2016-09-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 2018-11-16
    相关资源
    最近更新 更多