
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Checking for Not Null in MongoDB
Use $ne to check for not null. Let us create a collection with documents −
> db.demo764.insertOne({"LoginUserName":"Chris","LoginPassword":"Chris_12"}); { "acknowledged" : true, "insertedId" : ObjectId("5eb04ee55637cd592b2a4afc") } > db.demo764.insertOne({"LoginUserName":"Chris","LoginPassword":null}); { "acknowledged" : true, "insertedId" : ObjectId("5eb04eee5637cd592b2a4afd") } > db.demo764.insertOne({"LoginUserName":"Chris","LoginPassword":""}); { "acknowledged" : true, "insertedId" : ObjectId("5eb04ef35637cd592b2a4afe") }
Display all documents from a collection with the help of find() method −
> db.demo764.find();
This will produce the following output −
{ "_id" : ObjectId("5eb04ee55637cd592b2a4afc"), "LoginUserName" : "Chris", "LoginPassword" : "Chris_12" } { "_id" : ObjectId("5eb04eee5637cd592b2a4afd"), "LoginUserName" : "Chris", "LoginPassword" : null } { "_id" : ObjectId("5eb04ef35637cd592b2a4afe"), "LoginUserName" : "Chris", "LoginPassword" : "" }
Following is the query to check for not null −
> db.demo764.find({"LoginUserName":"Chris","LoginPassword" : { '$ne': null } });
This will produce the following output −
{ "_id" : ObjectId("5eb04ee55637cd592b2a4afc"), "LoginUserName" : "Chris", "LoginPassword" : "Chris_12" } { "_id" : ObjectId("5eb04ef35637cd592b2a4afe"), "LoginUserName" : "Chris", "LoginPassword" : "" }
Advertisements