The second argument to the db.collection.find() and db.collection.findOne() commands, also using JSON syntax, is the projection argument. The projection argument allows you to control which fields appear or do not appear in the final output.Â
Here is a summary of the projection options:
Projection expression | What appears in the output |
{ }Â (or blank) | All fields |
{ "field1" : 1 } | Only field1 and the _id fields |
{ "field1" : 0 } | All fields except for field1 |
{ "field1" : 1, "_id" : 0 } | Only field1 |
{ "field1" : 1, "field2" : 0 } | Not allowed! Cannot have a mixture of include and exclude (except for the _id field) |
For this example, we will assume that the management has asked for the name and email address of all customers from Quebec. Accordingly, you construct a query that uses the projection argument to only include fields pertinent to the name and address...