Query.prototype.findOne()
Parameters
[filter] «Object» mongodb selector
[projection] «Object» optional fields to return
[options] «Object» see
setOptions()
[callback] «Function» optional params are (error, document)
Returns:
- «Query» this
Declares the query a findOne operation. When executed, the first found document is passed to the callback.
Passing a callback
executes the query. The result of the query is a single document.
- Note:
conditions
is optional, and ifconditions
is null or undefined, mongoose will send an emptyfindOne
command to MongoDB, which will return an arbitrary document. If you’re querying by_id
, useModel.findById()
instead.
This function triggers the following middleware.
findOne()
Example
const query = Kitten.where({ color: 'white' });
query.findOne(function (err, kitten) {
if (err) return handleError(err);
if (kitten) {
// doc may be null if no document matched
}
});
当前内容版权归 mongoosejs 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 mongoosejs .