How to check if a field contains a null value? - pymongo

Sometimes my document has a field with null values.

  • How to check if a value inside a field is an empty string?
  • How to iterate over all documents and delete the document field if it has a zero string value?
  • Do I have to delete this field for this document so that I can do this to find an empty field? for i in v7.find({"eng":{"$exist":True}})

An example of my document structure is this:

{ "_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'}
+5
source share
1 answer

According to mongodb manuals, the following query:

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

The request returns only a document containing a null value:

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

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

+6

All Articles