I have a collection with >100,000 documents that contain multiple nested arrays. I need to Query based on a property located at the lowest level and return just the object at the bottom of the arrays.
Document Structure:
{
_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"
}
]
}
]
}
I have been using the Aggregation Pipeline to get the expected results which DOES WORK, however the performance is pretty terrible. Here is my query:
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
])
The Results are :
[
{
"type" : "employee",
"systemID" : 12345,
"item" : "RobsItemOne",
"value" : "$10.00"
},
{
"type" : "employee",
"systemID" : 12345,
"item" : "RobsItemTwo",
"value" : "$10.00"
}
]
What can I do to make this query faster? I have tried using indexes but per Mongo docs, indexes past the initial $match are ignored.