【发布时间】:2016-04-24 15:41:00
【问题描述】:
假设我有一个这样的文档集合,其中的 stock 字段包含一组嵌入文档:
{
_id: 1,
item: "abc",
stock: [
{ size: "S", color: "red", quantity: 25 },
{ size: "S", color: "blue", quantity: 10 },
{ size: "M", color: "blue", quantity: 50 }
]
}
我创建了一个这样的多键索引:
db.items.createIndex("stock.size")
但是当我跑步时:
db.runCommand({distinct: "items", key: "stock.size"})
它返回:
{
"values" : [
"S", "M"
],
"stats" : {
"n" : 1730969,
"nscanned" : 0,
"nscannedObjects" : 1730969,
"timems" : 13716,
"planSummary" : "COLLSCAN"
},
"ok" : 1
}
它执行了整个集合扫描并且没有使用索引。我可以以某种方式加速对子文档数组的不同查询吗?
谢谢
【问题讨论】: