collection.find
Selects documents in a collection and return them.
Arguments
query
(String|ObjectId|Object)[
options
] (Object|String|Array): If theoptions
is a string, it will be parsed as the fields to select. In addition to the mongo options, you can pass the optionrawCursor
in order to get the raw mongo cursor when the promise resolve.[
callback
] (function)
Returns
A promise with a each
method to stream the query.
The each
method expects a function which will receive two arguments:
doc
(Object): current document of the streamcursor
(Object):close
(function): close the stream. The promise will be resolved.pause
(function): pause the stream.resume
(function): resume the stream.
Example
users.find({}).then((docs) => {})
users.find({}, 'name').then((docs) => {
// only the name field will be selected
})
users.find({}, { fields: { name: 1 } }) // equivalent
users.find({}, '-name').then((docs) => {
// all the fields except the name field will be selected
})
users.find({}, { fields: { name: 0 } }) // equivalent
users.find({}, { rawCursor: true }).then((cursor) => {
// raw mongo cursor
})
users.find({}).each((user, {close, pause, resume}) => {
// the users are streaming here
// call `close()` to stop the stream
}).then(() => {
// stream is over
})