Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « How do you manage SQL Agent Jobs when using mirroring?WHERE not to use FUNCTIONS »
    comments

    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

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    2895 views
    InstapaperVote on HN

    No feedback yet

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)