Schema:
{
A: [
{ name: "string", age: "integer" },
{ name: "string", age: "integer" },
{ name: "string", age: "integer" },
...
]
}
compound index:
{
"A.name": 1,
"A.age": 1,
}
query
db.col.aggregate([
{ $match: { A: { $elemMatch: { name: "XYZ" } } } },
{ $sort: { "A.age": 1 } }
{ $set: ... }
...
{ $limit: 10 }
])
The above query will sort the array based on all of the subdocuments of A
.
How to get it to sort just based on the matching subdocuments? And then also to use the index?