正在加载...
|
Basically when using mongoose, documents can be retrieved using helpers. Every model method that accepts query conditions can be executed by means of a User.findOne({ name:'daniel'},function(err, user){//});
User.findOne({ name:'daniel'}).exec(function(err, user){//});
Therefore when you don't pass a callback you can build a query and eventually execute it. You can find additional info in themongoose docs. UPDATE Something to note when usingPromisesin combination with Mongoose async operations is that Mongoose queries arenotPromises. Queries do return athenable, but if you need arealPromise you should use the
Daniel has answered this quite beautifully. To elaborate on an exhaustive list of ways to build and execute queries, look at the following use cases: Query Building Mongoose will not execute a query until
Execution via callback Although disliked by many due to its nesting nature, queries can be executed by providing the optional callback.
Then API as a Promises/A+ Mongoose queries do provide a
The exec function From Mongoose docs
|
|
Basically when using mongoose, documents can be retrieved using helpers. Every model method that accepts query conditions can be executed by means of a User.findOne({ name:'daniel'},function(err, user){//});
User.findOne({ name:'daniel'}).exec(function(err, user){//});
Therefore when you don't pass a callback you can build a query and eventually execute it. You can find additional info in themongoose docs. UPDATE Something to note when usingPromisesin combination with Mongoose async operations is that Mongoose queries arenotPromises. Queries do return athenable, but if you need arealPromise you should use the |