【发布时间】:2014-10-27 18:41:01
【问题描述】:
我有一个包含超过 100,000 个文档的集合,其中包含多个嵌套数组。我需要根据位于最低级别的属性进行查询,并仅返回数组底部的对象。
文档结构:
{
_id: 12345,
type: "employee",
people: [
{
name: "Rob",
items: [
{
itemName: "RobsItemOne",
value: "$10.00",
description: "some description about the item"
},
{
itemName: "RobsItemTwo",
value: "$15.00",
description: "some description about the item"
}
]
}
]
}
我一直在使用聚合管道来获得可以正常工作的预期结果,但是性能非常糟糕。这是我的查询:
db.collection.aggregate([
{
$match: {
"type": "employee"
}
},
{$unwind: "$people"},
{$unwind: "$people.items"},
{$match: {$or: [ //There could be dozens of items included in this $match
{"people.items.itemName": "RobsItemOne"},
{"people.items.itemName": "RobsItemTwo"}
]
}
},
{
$project: {
_id: 0,// This is because of the $out
systemID: "$_id",
type: "$type",
item: "$people.items.itemName",
value: "$people.items.value"
}
},
{$out: tempCollection} //Would like to avoid this, but was exceeding max document size
])
结果是:
[
{
"type" : "employee",
"systemID" : 12345,
"item" : "RobsItemOne",
"value" : "$10.00"
},
{
"type" : "employee",
"systemID" : 12345,
"item" : "RobsItemTwo",
"value" : "$10.00"
}
]
我可以做些什么来加快这个查询?我尝试过使用索引,但根据 Mongo 文档,超过初始 $match 的索引将被忽略。
【问题讨论】:
标签: mongodb performance aggregation-framework nosql