Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Sometimes my document has a field that has null values.

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

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'}
like image 579
alvas Avatar asked Jan 31 '13 04:01

alvas


1 Answers

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

like image 119
cubbuk Avatar answered Sep 20 '22 16:09

cubbuk