How to check if a field is not null in mongodb?

beginer
beginer
1544 Points
52 Posts

I'm trying to query if a field is not null as

db.mycollection.find({"myfield" is not null});

What will be correct syntax to do this?

Views: 1674
Total Answered: 1
Total Marked As Answer: 0
Posted On: 03-Mar-2022 22:58

Share:   fb twitter linkedin
Answers
Smith
Smith
2890 Points
78 Posts
         

Use following to get all documents with both a key called "myfield" and a non-null value:

db.mycollection.find({"myfield":{$ne:null}});

Use following to get all documents with a key called "myfield", but they may still have a null value:

db.mycollection.find({"myfield":{$exists:true}});

 

Reference: https://docs.mongodb.com/manual/tutorial/query-for-null-fields/

 

Posted On: 04-Mar-2022 02:15
 Log In to Chat