Mongoose - What does the exec function do?

1.0K    Asked by JosephSlater in Spark , Asked on May 13, 2021

 I came across a piece of Mongoose code that included a query findOne and then an exec() function.

I've never seen that method in Javascript before? What does it do exactly?

Answered by Abdul Rehman

To understand the mongoose exec function basically when using mongoose, documents can be retrieved using helpers. Every model method that accepts query conditions can be executed by means of a callback or the exec method.

callback:
User.findOne({ name: 'daniel' }, function (err, user) {
//
});
exec:
User .findOne({ name: 'daniel' }) .exec(function (err, user) { //
});

Your Answer

Interviews

Parent Categories