Searching for documents in MongoDB
Once the documents have been inserted into the collection, they can be searched for using the find()
class method.
Note
The find()
method is a class method, which means that it is used by prefixing it with the class name associated with the model, for example, Client.find()
.
The find(conditions, callback)
method is used to perform a search in the collection associated with the model, then to retrieve the results of the search in the callback function indicated as a parameter.
Let’s take an in-depth look at the parameters:
- The
conditions
parameter is a JavaScript object used to specify search conditions. If no condition is specified, do not indicate anything (or indicate an empty object{}
). - The callback function is of the form
callback(err, results)
whereerr
is an error message (null
otherwise) andresults
is an array containing the search results (empty[]
if none).
There is also the findOne(conditions, callback...