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

user94628
  • 3,641
  • 17
  • 51
  • 88

4 Answers4

5

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

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

ltfishie
  • 2,917
  • 6
  • 41
  • 68
  • +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. – Oleg V. Volkov Dec 19 '12 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}}]})
Sushant Gupta
  • 8,980
  • 5
  • 43
  • 48
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 }}

slownage
  • 154
  • 1
  • 3
  • 11
0

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

Oleg V. Volkov
  • 21,719
  • 4
  • 44
  • 68