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

    « SSRS Properties - VerticalAlignListing all RESTORE and BACKUP operations currently going on your SQL Server »
    comments

    In a regular SQL database, you can check if a column is null by using IS NULL
    For example if you wanted to return all rows where the age is null, you would do the following

    1. SELECT *
    2. FROM SomeTable
    3. WHERE age IS NULL

    In a NoSQL database it is possible that half the documents in a collection are omitted and maybe five are there with the value null. How can you know if the field is missing or has the value null?

    Let's take a quick look. First insert the following document into your collection

    db.Blog.insert( { name : "Denis2" } )

    As you can see it just has a name. Now let's add another document this time with age as well, we will make the age NULL

    db.Blog.insert( { name : "Denis",  age : NULL} )

    You get the following error
    Sun Feb 17 13:44:58 ReferenceError: NULL is not defined (shell):1

    This error occurs because you need to pass null in lowercase

    db.Blog.insert( { name : "Denis",  age : null } )

    Now if you execute the following

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

    You get back both document
    { "_id" : ObjectId("512118a7c1eca3d7ffcd00f9"), "name" : "Denis", "age" : null }
    { "_id" : ObjectId("512123d9c1eca3d7ffcd00fa"), "name" : "Denis2" }

    In order to return the document where the value that is stored is null, you can use $type: 10. What that means is that the field is of BSON Type Null

    db.Blog.find( { age: { $type: 10 } } )

    Here is the output

    { "_id" : ObjectId("512118a7c1eca3d7ffcd00f9"), "name" : "Denis", "age" : null }

    In order to return the document where the field does not exist, you can use $exists: false

    db.Blog.find( { age: { $exists: false } } )

    Here is the output

    { "_id" : ObjectId("512123d9c1eca3d7ffcd00fa"), "name" : "Denis2" }


    That is all for this post, 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
    MongoDB: How to include and exclude the fields you want in results
    MongoDB: How to limit results and how to page through results
    MongoDB: How to backup and restore databases
    MongoDB: How to restore collections
    MongoDB: How to backup all the databases with one command
    MongoDB: Exporting data into files
    MongoDB: How to drop databases and collections
    MongoDB: Creating capped collections

    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
    1982 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.)