I’m working with a MongoDB collection where documents contain an array of subdocuments. For example:
{
_id: 1,
items: [
{ type: "A", value: 10 },
{ type: "B", value: 20 }
]
}
I’ve created an index on the subdocument field:
db.test.createIndex({ "items.type": 1 })
Since items is an array of subdocuments, this is a multikey index. My understanding is that MongoDB will create multiple index keys for a single parent document (e.g., one for “A”, one for “B”).
My question is:
-
When a query uses this index, and multiple array elements match, does MongoDB return duplicate parent documents?
-
For example:
db.test.find({ "items.type": { $in: ["A", "B"] } })
Will the result contain _id: 1 only once, or will it appear twice since two index keys match?
I’m trying to confirm how MongoDB handles deduplication when scanning multikey indexes.