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.
Related Whitepaper:

Open Source Data Management for Big Data and NoSQL

Join Talend for this new on-demand webinar to show how data management can benefit your organization.

This on-demand webinar shows how Talend for Big Data greatly simplifies the process of working with Hadoop and NoSQL and makes Big Data integration easy, fast, and affordable.

Get it Now!  

Leave a Reply


+ 4 = eight



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
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.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books
Get tutored by the Geeks! JCG Academy is a fact... Join Now
Hello. Add your message here.