Skip to content

Instantly share code, notes, and snippets.

@gatesvp
Created May 4, 2011 18:30
Show Gist options
  • Select an option

  • Save gatesvp/955721 to your computer and use it in GitHub Desktop.

Select an option

Save gatesvp/955721 to your computer and use it in GitHub Desktop.
Test for nulls in MongoDB
> db.foo.insert( { x : 1, y : 1 } )
> db.foo.insert( { x : 2, y : "string" } )
> db.foo.insert( { x : 3, y : null } )
> db.foo.insert( { x : 4 } )
> db.foo.find()
{ "_id" : ObjectId("4dc1974612c677fc83b5629d"), "x" : 1, "y" : 1 }
{ "_id" : ObjectId("4dc1974c12c677fc83b5629e"), "x" : 2, "y" : "string" }
{ "_id" : ObjectId("4dc1975312c677fc83b5629f"), "x" : 3, "y" : null }
{ "_id" : ObjectId("4dc1975a12c677fc83b562a0"), "x" : 4 }
> db.foo.find( { "y" : null } )
{ "_id" : ObjectId("4dc1975312c677fc83b5629f"), "x" : 3, "y" : null }
{ "_id" : ObjectId("4dc1975a12c677fc83b562a0"), "x" : 4 }
> db.foo.find( { "y" : { $type : 10 } } )
{ "_id" : ObjectId("4dc1975312c677fc83b5629f"), "x" : 3, "y" : null }
> db.foo.find( { "y" : { $exists : false } } )
{ "_id" : ObjectId("4dc1975a12c677fc83b562a0"), "x" : 4 }
>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment