MongoDB find all with value of undefined

Viewed 5505

Hi I have accidently update some of the records in mongodb with a value of undefined by writing some vanila javascript inside NoSQLBooster IDE. Problem is that records exist but when I ran $exists to true, it shows the records in Robo3T but not in NoSQLBooster. How ever when I ran email is equal to undefined it doesn't return any records however records exist. Here is the sample query and screenshot of result

 db.customer.find({source:'some value',"email":{$exists: true}})
   .projection({})
   .sort({_id:-1})

enter image description here

How do I query and correct those records. Thanks

1 Answers

You can query for the documents where email is undefined by using the $type operator:

db.customer.find({source: 'some value', email: {$type: 'undefined'}})

Note that the undefined type is listed as deprecated in the docs, but it still works for now.

Querying for email: null will also return these documents, but that will also include documents where email is missing or set to null. As such, you'd need to further qualify the query with two more terms to exclude those:

db.customer.find({$and: [
    {source: 'some value'},
    {email: null}, 
    {email: {$not: {$type: 'null'}}}, 
    {email: {$exists: true}}]})
Related