4

I'm trying to run a MongoDB query and return those records where a field is null (more specifically None in pyMongo). So it has to be equal to null.

I know this is not equal to:

{"firstName": {"$ne": None }}

I can't find the equal operator in the documentation.

Thanks

1

4 Answers 4

5

{"firstName":{ $type: 10 } } should give you what you want

http://docs.mongodb.org/manual/faq/developers/#faq-developers-query-for-nulls

1
  • +1 not for the answer, but for the link that covers all possibilities. It'd be good to have concise version of it summarized in answer directly. Commented Dec 19, 2012 at 15:38
2

If you want to find records having firstName defined in record with value None defined:

db.testcoll.find({$and: [{"firstName": None}, {"firstName": {$exists: true}}]})
1

If I understand correctly it should be just:

{"firstName": None}

With yours you are getting all the documents that have a value different of None. {"firstName": {"$ne": None }}

1
  • I think this matches documents with no firstName as well; not what the OP is asking for. Commented Dec 19, 2012 at 15:38
0

Use {"firstName": { "$exists": false }} to find records where there's no such field.

3
  • But it would find fields where firstName doesn't even exist. Commented Dec 19, 2012 at 15:35
  • 1
    Indeed. I've just upvoted answer with link to FAQ which cover all of possibilities. Commented Dec 19, 2012 at 15:37
  • Yup, very nice and elegant solution. I liked it :) Commented Dec 19, 2012 at 15:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.