alvas
alvas

Reputation: 122270

How do I check whether a field contains null value? - pymongo

Sometimes my document has a field that has null values.

An example of my document structure is as such:

{ "_id" : ObjectId("50fd55c1161747668078fed6"), 
"dan" : "Hr. Hänsch repræsenterede Dem dér .", 
"deu" : "Der Kollege Hänsch hat Sie dort vertreten .", 
"eng" : "Mr Hänsch represented you on this occasion .", 
"fin" : "Kollega Hänsch edusti teitä siellä .", 
"ita" : "Il collega Hänsch è intervenuto in sua vece .", 
"md5" : "336b9cd1dc01ae0ff3344072d8db0295", 
"uid" : 2100986 }

{u'uid': 104, 
u'fre': u"Je crois que la pr\xe9vention est d' une importance capitale , et ce non pas \xe0 cause des criminels \xe0 la t\xeate du pouvoir .", 
u'eng': u'', 
u'dan': u'Som hr .', 
u'deu': u'.', 
u'ita': u"L' emendamento n .", 
u'nld': u'Dat gebied is de Balkan van de toekomst , een Balkan op wereldschaal .', 
u'spa': u'\xa1 Y no por los criminales que se hallan al frente de las instituciones !', 
u'_id': ObjectId('50fd381f161747668058f043'), 
u'fin': 
u'.', u'md5': u'd41d8cd98f00b204e9800998ecf8427e'}

Upvotes: 6

Views: 3772

Answers (1)

cubbuk
cubbuk

Reputation: 7920

According to manuals of mongodb the following query achieve this:

db.test.find( { eng : { $type: 10 } } )

The query returns only the document that contains the null value:

The { field : { $type: 10 } } query matches documents that contains the field whose value is null only; i.e. the value of the field is of BSON Type Null (i.e. 10)

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

Upvotes: 6

Related Questions