MongoDB Projection
Introduction
Sometimes, as a DBA or technical administrator, you may only want to see records that match certain attributes. When you have a need to do that, MongoDB projection is a useful operator you’ll want to try. With projection, you can query a collection to return those documents with the fields you specify. Any collection document that doesn’t contain the fields you identify in your query are excluded from the results.
Let’s take a closer look. Typically, when querying a MongoDB document collection, you’ll use the JavaScript find()
array. It returns every document. In the example shown below, a query was executed for the collection of documents containing football player data. The collection is named “myteam” and it contains six documents.
Notice the fields for “Name,” “Club,” and “Country”. All of the documents that were returned show their particular string values. That’s what usually happens which is exactly as it should. However, if you wanted to return, say, just the documents with values that match a specific element for the “Club” query and not all of the clubs in that document collection, you could do that too.
The example below adds the string element “Barcelona” to the query “club.”
1 | db.myteam.find({ club: "Barcelona" }); |
Below, is the response. The documents that match the element “Barcelona” are returned.
The above example returns only those documents that match the specific query.
Projection
MongoDB projection does more than return documents that match a specific field in a query. There may be times when you’ll want to display documents that return data for just one of several fields in the array.
Our myteam collection shows documents that can return three fields for:
- Name
- Club
- Country
Use MongoDB projection to select a specific field in an array and limit the documents returned to exactly what you want to see.
For instance, we’ll still create a query using projection that will return all of the documents in the “myteam” collection. This time though, we’ll want to limit the fields returned so that just the football player’s names are displayed with their corresponding document IDs.
You would use the find() method exactly as shown in the example below.
>NOTE: In this example, we skipped identifying the array’s first argument because this happens automatically when nothing is specified. When executed, it will return all available documents in the named “myteam” database collection.
1 | db.myteam.find({}); |
>NOTE: An empty query is indicated by an empty object.
A MongoDB projection will be passed as the second argument.
1 | { "name" : 1 } |
Shown above, “Name” is inputted as the MongoDB projection. When a projection is used, it excludes all other fields. Only the documents having fields with values for “Name” will be displayed along with their related document ID numbers. In this case, it’s the entire collection of six documents since each document in the “myteam” collection contains a football player’s name.
In the mongo shell, a result like this is returned:
It was completed successfully. As desired, the club and country and matching elements for the fields are missing.
You can also choose not to display the document ID numbers. Specify this by adding “ID” as an additional projection along with setting the field to “0” as shown below.
1 | { "name" : 1, "_id" : 0 } |
Below displays the correct result.
You can add more than one MongoDB projection too. Here, we’ll change the script to show just the “Name” and “Club” fields on all documents. We want to omit both country and ID fields in our display.
1 | { "name" : 1, "club" : 1 ,"_id" : 0 } |
This is what you should see as a response.
Conclusion
MongoDB projection allows DBAs to select one or more fields in a document collection and just have those specific fields of the documents returned in the response. In short, you can omit any irrelevant data that is not needed to display. This means that you’ll streamline productivity every time you’re able to precisely identify what you want and then quickly obtain that information. What’s more, when you have a large collection, it can make a huge difference in your data retrieval efforts. But even if your database is relatively small, the data within it will likely change and grow. Therefore, implementing strategic ways now to manage MongoDB collections to save time is a worthwhile ongoing goal for which to strive.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started