我有一个相当小的63k文件(总共2.5GB)的数据集。文档示例:
{
_id : "[uniqueId]",
FormId : 10,
Name : "Name of form",
IsComplete : true,
Sections : [ many sections and can be large ]
}
我想通过FormId获取文档的总数。我对这个查询得到了快速的结果(.15sec):
db.getCollection('collection').aggregate([
{ $sort : { FormId : 1 } }, //Index exists on FormId
{ $group : { _id : "$FormId", count : { $sum : 1 } } },
{ $sort : { "count" : -1 } }
])
我的问题是我需要的文件,其中{“IsComplete”:真正}的计数。我有两个建立在两个属性上的索引,但是我意识到使用$ match运算符会扫描所有文档。那么如何有效地过滤$组计数?
这里你不需要第一个'$ sort'阶段。 – styvane
@Styvane没有$ sort阶段$组需要10秒钟才能完成。为什么会发生?从$ sort开始使用索引。 – JayJohnsonDesigns