Database Reference
In-Depth Information
Using projection
Another approach to perform faster queries in MongoDB is to use the projection feature.
With projection, you limit the output of the database to specific fields and remove the re-
dundant data from the result. Thus, the output of the database engine will be smaller and
the client can fetch and download the result faster than before.
The use of the projection feature is as easy as passing a collection to the second parameter
of the find method, which is similar to the following command:
db.testcollection.find({}, {_id: 0})
For instance, in the preceding command, the engine returns all documents but it excludes
the _id field.
The same as in the previous example, the following command returns records with a value
greater than 500 for the field x . All the records contain only the field x as shown in the
following command line:
db.testcollection.find({ x: {$gt: 500} }, {x: 1, _id:0})
It is worth mentioning that passing the value 0 to the projection option excludes the field
from the final result and passing 1 includes it.
Search WWH ::




Custom Search