Using Projection for Clarity and Focus

Introduction

Welcome to the first lesson of the course "A Closer Look at Read Operations". This course builds upon the foundations laid out in previous courses, where you learned the basic concepts of MongoDB, CRUD operations, data modeling, and relationships. In this course, we will focus exclusively on read operations and data retrieval, specifically tackling complex retrieval scenarios. In this lesson, we will delve into projection.

What is Projection?

Projection in MongoDB allows you to specify which fields you want to include or exclude in your query results. This can be crucial for performance and efficiency, especially when dealing with large documents, as it reduces the amount of data transmitted over the wire. Instead of retrieving the entire document, you can hone in on just the information you need.

For example, consider this document:

{
  "title": "The Amazing Spider-Man",
  "issue_number": 1,
  "published_year": 1963,
  "genres": ["Action", "Adventure", "Superhero"],
  "writer": { "name": "Stan Lee", "nationality": "American" },
  "artist": { "name": "Steve Ditko", "nationality": "American" },
  "characters": [
     { "name": "Spider-Man", "alter_ego": "Peter Parker", "abilities": ["Super strength", "Wall-crawling", "Spider-sense"] },
     { "name": "J. Jonah Jameson", "role": "Editor-in-Chief of the Daily Bugle" },
     { "name": "Aunt May", "role": "Peter Parker's Aunt" }
  ],
  "publisher": { "name": "Marvel Comics", "location": "New York" },
  "related_comics": [ "Spider-Man: Blue", "Ultimate Spider-Man", "Spider-Verse" ]
}

If your application only needs the title and published_year for a listing page, there’s no need to retrieve the entire document.

Syntax Explained

Let's use the findOne method to demonstrate projection. Imagine that you want to retrieve a document from the comic_books collection, but you are only interested in the title and published_year fields. Here's the query you can use:

JavaScript
use comic_book_store_db

db.comic_books.findOne({}, { title: 1, published_year: 1, _id: 0 })

This query will return the first document in the collection with the specified fields only:

{ "title": "The Amazing Spider-Man", "published_year": 1963 }

In this query, the first argument to the findOne method is the query filter, represented by an empty object {}, which matches all documents in the collection. The second argument is the projection object { title: 1, published_year: 1, _id: 0 }. Here, title: 1 and published_year: 1 specify that these fields should be included in the result, while _id: 0 indicates that the _id field should be excluded from the result. By default, MongoDB includes the _id field in the output, so explicitly excluding it is necessary if you don't need it. Instead of using 1 to include and 0 to exclude fields, you can also use true and false, respectively.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal