
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
MongoDB checking for not null?
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" : "" }
- Related Articles
- Checking for Null or Empty in Java.
- Check for null in MongoDB?
- How to query for records where field is null or not set in MongoDB?
- Checking for Null or Empty or White Space Only String in Java.
- MongoDB query which represents not equal to null or empty?
- Check for NULL or NOT NULL values in a column in MySQL
- Is their JavaScript “not in” operator for checking object properties?
- Set 1 for NOT NULL value in MySQL
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- MongoDB query to select one field if the other is null and the first field if both are not null?
- Querying null value in MongoDB?
- Difference Between MySql NULL and IS NOT NULL?
- MongoDB Query for boolean field as “not true”
- Select MongoDB documents where a field either does not exist, is null, or is false?
- Working with NULL and IS NOT NULL in MySQL

Advertisements