# 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?

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