MongoDB - Return Only Selected Fields

We begin this next chapter by discussing the MongoDB projection document. In order to select only those fields you want returned by your query, the projection document can define those fields to be either explicitly included or excluded.

In MongoDB, the projection document has the following document structure:

field name : True or 1
To be included in the return result

field name : False or 0
To be excluded in the return result


The MongoDB id field is, by default, included in the return set. To prevent the id field from appearing in the result, enter id: 0 in the projection document.

It should also be noted that you can not include and exclude fields in a single projection with the exception of the id field.

Selecting Which Fields to Return in Our Query

In our example projection document, we enter the MongoDB shell...

connecting to test  
> db.scores.find({ 'type' : 'essay'}, { 'score' : 1, 'test_date' : 1 });

In the above query, we asked MongoDB to find in our scores collection, items that match the score type and; only return the score and the date the score was recorded.

In the same example, we can tell MongoDB to not include the id field:

connecting to: test  
> db.scores.find({ 'type' : 'essay'}, { 'score' : 1, 'test_date' : 1, '_id' : 0 });

While we are limiting fields in our query, we can also exclude only a single field, so that MongoDB returns all of the fields except the one specifically excluded.

connecting to: test  
> db.scores.find({ 'type' : 'essay'}, { 'type' : 0 });

This will display all of our score data except the score type.

What About Fields in Embedded Documents

If you recall from a previous chapter on MongoDB, we can use dot notation to dig deep into embedded documents in our collection.

connecting to test  
> db.people.find({ 'role' : 'admin'}, { 'address.state' : 1, 'address.zip_code' : 1 });
Limiting Fields When Projecting an Array

Many MongoDB collections contain fields that are arrays and lists. This is one of the many benefits of schema-less databases. For fields that contain arrays or lists, MongoDB provides us with projection operators to use to find our data.

The MongoDB projection operators for Arrays and Lists are:

  • $elemMatch
  • $slice
  • $

In a document containing an array like this...

  '_id' : ObjectId('5641e...'),
  'type' : 'fruit',
  'item_number' : 'AFRE1876TFR',
  'item_name' : 'Delmonica Apple',
  'tags' : ['produce', 'market', 'fresh', 'apples']

We could slice the array and run a find query to look inside of the tags array, which is basically a simple list.

db.fruit.find({'_id' : 5}, { 'tags' : { $slice : 'apples' }});  

These are the only methods MongoDB provides for projection and array or list.

Take special note that you absolutely can not select a part of an array by using the array index.

This is not allowed. You must use $elemMatch, $slice and $.

Craig Derington

Secular Humanist, Libertarian, FOSS Evangelist building Cloud Apps developed on Red Hat Enterprise Linux and Ubuntu Server. My toolset includes Python, Celery, Flask, Django, MySQL, MongoDB and Git.

comments powered by Disqus