I tweaked your query just a bit (adding a match to the front since I don't want to unwind the Tags array for all document):
db.collection.aggregate(
[
{ $match: {$or: [{"Tags._id":"tag1"},{"Tags._id":"tag2"}]}},
{ $unwind : "$Tags" },
{ $match: {$or: [{"Tags._id":"tag1"},{"Tags._id":"tag2"}]}},
{ $group: { _id : "$_id", count: { $sum:1 } }},
{$sort: {"count":-1}}
],
{ explain: true }
)
And got:
{
"stages" : [
{
"$cursor" : {
"query" : {
"$or" : [
{
"Tags._id" : "tag1"
},
{
"Tags._id" : "tag2"
}
]
},
"plan" : {
"cursor" : "BtreeCursor ",
"isMultiKey" : false,
"scanAndOrder" : false,
"indexBounds" : {
"Tags._id" : [
[
"tag1",
"tag1"
],
[
"tag2",
"tag2"
]
]
},
"allPlans" : [
{
"cursor" : "BtreeCursor ",
"isMultiKey" : false,
"scanAndOrder" : false,
"indexBounds" : {
"Tags._id" : [
[
"tag1",
"tag1"
],
[
"tag2",
"tag2"
]
]
}
}
]
}
}
},
{
"$unwind" : "$Tags"
},
{
"$match" : {
"$or" : [
{
"Tags._id" : "tag1"
},
{
"Tags._id" : "tag2"
}
]
}
},
{
"$group" : {
"_id" : "$_id",
"count" : {
"$sum" : {
"$const" : 1
}
}
}
},
{
"$sort" : {
"sortKey" : {
"count" : -1
}
}
}
],
"ok" : 1
}
While this doesn't quite address why your operation returns a planError, but maybe it can help some how.
Regards