O'Reilly logo

The Node Craftsman Book by Manuel Kiessling

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required