MongoDB需要很长时间才能查询

时间:2015-10-16 18:53:43

标签: mongodb

我有一个超过10000个文件的集合。每份文件大约10KB。 当我运行此查询时:

News
.find({topics: { '$elemMatch': { '$in': ['5606059d924327636fe3e150'] } }, state: 'APPROVED', is_removed: false})

完成查询需要大约8秒钟。

我使用了字段的索引:topics,state和is_removed。 explain()查询返回:“millis”:45。

我认为查询的结果非常大(10000 * 10KB)所以需要时间来抽取数据吗?

请帮我解释一下,并告诉我如何缩短查询时间。

谢谢!

1 个答案:

答案 0 :(得分:1)

对于初学者:您的查询不必要地复杂。

db.collection.find({
    topics:{ "$in":["5606059d924327636fe3e150","5606059d924327636fe3e151"]},
    state:"APPROVED",
    is_removed: false
})

此外,您可能想要一个索引:

db.collection.createIndex({ topics:1, state:1, is_removed:1 })