Test empty string in mongodb and pymongo

MongodbPymongo

Mongodb Problem Overview


Here is my data structure.

[{"name": "David","lastname": "",},{"name": "Angela"}]

"lastname" is sometimes present and sometimes not and sometime is "".

I want to get all rows that have lastname not equal to "". But this does not work. It returns both the rows when lastname is "" and when lastname is not present at all. in the example above I want to only get the David node.

db.collection.find( {"lastname": {"$ne": ""}} )

Mongodb Solutions


Solution 1 - Mongodb

db.collection.find({"lastname" : {"$exists" : true, "$ne" : ""}})

In the mongo shell (id's omitted to save space)

> db.collection.find()
  { "name" : "Angela" }
  { "name" : "David", "lastname" : "" }
  { "name" : "Kyle",  "lastname" : "Test" }
  { "name" : "John",  "lastname" : null }

> db.collection.find({"lastname" : {"$exists" : true, "$ne" : ""}})
  { "name" : "Kyle", "lastname" : "Test" }
  { "name" : "John",  "lastname" : null }

In case you also want to filter out matches against null values you need to adjust the criteria as follows (we can also get rid of $exists as "$ne": null takes care of this)

> db.collection.find({$and:[{"lastname": {"$ne": null}}, {"lastname": {"$ne": ""}}]})
  { "name" : "Kyle", "lastname" : "Test" }

Solution 2 - Mongodb

Facing this problem I thought in another solution:

db.collection.find({"lastname": {"$gte": " "}})

With this I could get only the not empty strings, also ignoring null and not existent field, because any printable value (ASCII) has a greater value than space (32).

https://en.wikipedia.org/wiki/ASCII

Solution 3 - Mongodb

You can use a regex query:

db.test.find({ "lastname": /(.|\s)*\S(.|\s)*/ })

This regex matches strings beginning or ending with 0 or N whitespaces (.|\s) and it have to be one or more non-whitespaces \S in the middle.

Solution 4 - Mongodb

I'm not sure if this helps, but it has worked for me. The regex .+ returns anything that contains more than 1 character, whereas .* returns 0 or more. So it won't return anything less than 0 characters.

In terms of strings containing only whitespace, I don't think this solution can handle that.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid DehghanView Question on Stackoverflow
Solution 1 - MongodbKyleView Answer on Stackoverflow
Solution 2 - MongodbJoao Roberto joaogameprogView Answer on Stackoverflow
Solution 3 - MongodbMarcos GodinhoView Answer on Stackoverflow
Solution 4 - MongodbJadboy20View Answer on Stackoverflow