DEV Community

Discussion on: How to use Populate in Mongoose & Node.js

Collapse
 
ishubham_g profile image
Shubham Gupta

does the populate use find() query behind the scenes and if it does, will it also activate all the pre query middlewares of the model whose documents are used to populate?
Suppose .populate('comments')
will it also run the pre query middlewares of the ref of comments?

Collapse
 
paras594 profile image
Paras 🧙‍♂️

Yes we can say it makes a call to find method behind the scenes. But I don't know about pre query middlewares. You have to try it once to see the outcome. Maybe it will pre query middlewares. Never really tried :)

Collapse
 
ishubham_g profile image
Shubham Gupta

I have another question, that we also have the option to select certain fields while using populate like
.populate({path: 'comments', select: 'content name'}). Do you have some idea that it only summons that selected field or summons the whole documents and then selects the field?

Thread Thread
 
paras594 profile image
Paras 🧙‍♂️

Honestly I don't have idea about this one. But I can say that returning whole docs is easier because it doesn't have to convert it to partial one and return BSON data directly. So selecting specific fields adds an overhead. Don't let it discourage you from using select in populate or find.