Here I answer what is the difference between .exec() and .execPopulate() in Mongoose?
- .exec() is used with a query while .execPopulate() is used with a document
Syntax for .exec() is as follows:
Model.query()
.populate('field')
.exec() // returns promise
.then(function (document) {
console.log(document);
});
Syntax for .execPopulate() is as follows:
fetchedDocument
.populate('field')
.execPopulate() // returns promise
.then(function (document) {
console.log(document);
});
When working with individual document use .execPopulate(), for model query use .exec(). Both returns a promise. One can do without .exec() or .execPopulate() but then has to pass a callback in populate.
thanks, understood the difference in first line itself.
ReplyDelete"exec() is used with a query while .execPopulate() is used with a document"