In this MongoDB post we are going to look at how to return only the fields you want. We already looked at how to sort the results in the MongoDB, how to sort results post but we didn’t show you how to return just the fields you want.

To get started insert the following into your MongoDB database

db.Blog.insert( { name : "Denis",  age : 20, city : "Princeton" } )
db.Blog.insert( { name : "Abe",    age : 30, city : "Amsterdam" } )
db.Blog.insert( { name : "John",   age : 40, city : "New York"  } )
db.Blog.insert( { name : "Xavier", age : 10, city : "Barcelona" } )
db.Blog.insert( { name : "Zen",    age : 50, city : "Kyoto"     } )

To return everything, you would execute the following

db.Blog.find()

Here are the results

{ "_id" : ObjectId("51028ae0a8c33b71ed76a807"), "name" : "Denis", "age" : 20, "city" : "Princeton" }
{ "_id" : ObjectId("51028ae0a8c33b71ed76a808"), "name" : "Abe", "age" : 30, "city" : "Amsterdam" }
{ "_id" : ObjectId("51028ae2a8c33b71ed76a809"), "name" : "John", "age" : 40, "city" : "New York" }
{ "_id" : ObjectId("51028ae2a8c33b71ed76a80a"), "name" : "Xavier", "age" : 10, "city" : "Barcelona" }
{ "_id" : ObjectId("51028ae4a8c33b71ed76a80b"), "name" : "Zen", "age" : 50, "city" : "Kyoto" }

To return the name and not age or city, you would execute this

db.Blog.find(null, {name: 1});

Here are the results

{ "_id" : ObjectId("51028ae0a8c33b71ed76a807"), "name" : "Denis" }
{ "_id" : ObjectId("51028ae0a8c33b71ed76a808"), "name" : "Abe" }
{ "_id" : ObjectId("51028ae2a8c33b71ed76a809"), "name" : "John" }
{ "_id" : ObjectId("51028ae2a8c33b71ed76a80a"), "name" : "Xavier" }
{ "_id" : ObjectId("51028ae4a8c33b71ed76a80b"), "name" : "Zen" }

To return the name and age but not city, you would execute this

db.Blog.find(null, {name: 1, age: 1});

Here are the results

{ "_id" : ObjectId("51028ae0a8c33b71ed76a807"), "name" : "Denis", "age" : 20 }
{ "_id" : ObjectId("51028ae0a8c33b71ed76a808"), "name" : "Abe", "age" : 30 }
{ "_id" : ObjectId("51028ae2a8c33b71ed76a809"), "name" : "John", "age" : 40 }
{ "_id" : ObjectId("51028ae2a8c33b71ed76a80a"), "name" : "Xavier", "age" : 10 }
{ "_id" : ObjectId("51028ae4a8c33b71ed76a80b"), "name" : "Zen", "age" : 50 }

Do you notice that _id is always returned? What if you want to exclude _id from the results? You can exclude _id by adding _id: 0

Here is how you return just the name

db.Blog.find(null, {name: 1, _id: 0});

Results are below

{ "name" : "Denis" }
{ "name" : "Abe" }
{ "name" : "John" }
{ "name" : "Xavier" }
{ "name" : "Zen" }

Here is how you return just the name and the age

db.Blog.find(null, {name: 1, age: 1, _id: 0});

Here are the results

{ "name" : "Denis", "age" : 20 }
{ "name" : "Abe", "age" : 30 }
{ "name" : "John", "age" : 40 }
{ "name" : "Xavier", "age" : 10 }
{ "name" : "Zen", "age" : 50 }

As you can see this is a little different from SQL, if you in SQL do something like SELECT name, age FROM Table, you won’t get the id or primary key back in the results by default

If you are interested in my other MongoDB posts, you can find them here:
Install MongoDB as a Windows Service
UPSERTs with MongoDB
How to sort results in MongoDB
Indexes in MongoDB: A quick overview
Multidocument updates with MongoDB