Home > front end >  does $unwind preserve indexes if array field has multi key index?
does $unwind preserve indexes if array field has multi key index?

Time:12-27

{
  A: [ { B: "number", C: "number" } ]
}

Supposed that there is a multi key index on A. Does the query below preserve that?

db.col.aggregate( [ { $unwind: "$A" }, { $match: { } } ] )

CodePudding user response:

No, the indexes will not be preserved.

Indexes in aggregate() can only be used in the beginning of the pipeline, and not after the data has been transformed.

For example, you can leverage indexes with $match stage, if it's the first in the Aggregation pipeline. But if you put $match after the $unwind, it can not leverage the indexes since you already transformed the data.

  • Related