Retrieving specific documents using filters

We have not yet performed any truly interesting queries against our collection. Up until now, all we did was to retrieve all documents contained in our collection by performing collection.find() without any filter. More complex filters are available of course.

Let's rewrite the existing code in order to retrieve only specific documents in our doFind function. First, a very simple example – we are going to retrieve all documents where the v attribute has a value of 5. Let's change the following line:

collection.find().toArray(function (err, documents) {

The preceding line is changed to:

collection.find({'v': 5}).toArray(function (err, documents) {

The preceding changes result in only the matching ...

Get The Node Craftsman Book now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.