Mongodb – Understanding MongoDB Aggregation

mongodb

Pretend that our collection has 1M document
Now if i do aggregation like this:

db.users.aggregate({
    $group: {
        _id : "$gender",
        avgAge : { $avg : "$age" }
    }
})

then how it will prepare the data for me?
Say i have a index on the id but not on the gender so is it gonna read all the 1M documents? if so then how does it scale?

Best Answer

Indices can speed up an aggregation when utilized in a $match or a $sort stage at the beginning of a pipeline.

With your example, no index will be used, since you don't use either. So every document will be accessed.