MongoDB Pro Tip: Field Projections

Did you ever learn that select * from table in RDBMS-land is bad? Of course, you did! If you’re only looking for the email address of a user and not the other 15 columns worth of data, then why ask for that data and incur a penalty? The query select email from user where user_id = 1; is far more efficient for the database and the corresponding application that issued it, because there is less data to fetch and consume.

As it turns out, the same rule of thumb is true in MongoDB-land. That is, db.users.find({user_id:1}) is just as inefficient as the select * query if all you want is the user’s email address. With MongoDB, you can specify a projection as a part of your query that ultimately can limit what fields come back.

Thus, if I only want the email field on a user, I can issue a query like so:

MongoDB field projection:

db.users.find({user_id:1}, {email:1})

The second clause specifies that you only want the email field returned. You can also negate fields by issuing a 0, which means false. To negate first_name and last_name, you would type:

MongoDB field negation with 0 or false:

db.users.find({user_id:1}, {first_name:0, last_name:0})

In this case, I’d get all fields on that user document except first_name and last_name. Note, you cannot issue both an include and exclude in the same statement.

For you Mongoid users, including specific fields translates to only and excluding them translates to without – each clause can be attached to a criteria (but not both at the same time). For example, if User is a Mongoid document and I only want an underlying query to grab the email field, then the corresponding Mongoid query would be:

Mongoid field projection:

User.where(user_id:1).only(:email)

Field projection reduces document sizes on a fetch – this decreases memory consumption (for example, in the case of Mongoid your models aren’t fully populated with data) as well as bandwidth (that is, document retrieval is faster). Both MongoDB and the calling application benefit from field projection – it’s a win-win all the way around.
 

Reference: MongoDB Pro Tip: Field Projections from our JCG partner Andrew Glover at the The Disco Blog blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


two + = 9



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close