
- 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 query which represents not equal to null or empty?
To set a query for not equal to null or empty, use the $nin operator. The syntax is as follows
db.yourCollectionName.find({yourFieldName:{$nin:[null,""]}});
Let us create a collection with documents
> db.notEqualToNullOrEmptyDemo.insertOne({"UserName":"Larry","UserAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d20b6a629b87623db1b26") } > db.notEqualToNullOrEmptyDemo.insertOne({"UserName":"","UserAge":29}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d20bea629b87623db1b27") } > db.notEqualToNullOrEmptyDemo.insertOne({"UserName":"Sam","UserAge":32}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d20c7a629b87623db1b28") } > db.notEqualToNullOrEmptyDemo.insertOne({"UserName":null,"UserAge":27}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d20d2a629b87623db1b29") } > db.notEqualToNullOrEmptyDemo.insertOne({"UserName":"Robert","UserAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d20dda629b87623db1b2a") } > db.notEqualToNullOrEmptyDemo.insertOne({"UserName":"","UserAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d20e7a629b87623db1b2b") }
Following is the query to display all documents from a collection with the help of find() method
> db.notEqualToNullOrEmptyDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9d20b6a629b87623db1b26"), "UserName" : "Larry", "UserAge" : 24 } { "_id" : ObjectId("5c9d20bea629b87623db1b27"), "UserName" : "", "UserAge" : 29 } { "_id" : ObjectId("5c9d20c7a629b87623db1b28"), "UserName" : "Sam", "UserAge" : 32 } { "_id" : ObjectId("5c9d20d2a629b87623db1b29"), "UserName" : null, "UserAge" : 27 } { "_id" : ObjectId("5c9d20dda629b87623db1b2a"), "UserName" : "Robert", "UserAge" : 26 } { "_id" : ObjectId("5c9d20e7a629b87623db1b2b"), "UserName" : "", "UserAge" : 23 }
Following is the query to set condition for not equal to null or empty
> db.notEqualToNullOrEmptyDemo.find({UserName:{$nin:[null,""]}}).pretty();
This will produce the following output
{ "_id" : ObjectId("5c9d20b6a629b87623db1b26"), "UserName" : "Larry", "UserAge" : 24 } { "_id" : ObjectId("5c9d20c7a629b87623db1b28"), "UserName" : "Sam", "UserAge" : 32 } { "_id" : ObjectId("5c9d20dda629b87623db1b2a"), "UserName" : "Robert", "UserAge" : 26 }
- Related Articles
- How to query for records where field is null or not set in MongoDB?
- MySQL query to convert empty values to NULL?
- MySQL query to check if database is empty or not?
- Which one is better in MySQL - NULL or empty string?
- Which one is better to insert NULL or empty string in MySQL?
- How to test String is null or empty?
- MongoDB checking for not null?
- Checking for Null or Empty in Java.
- MySQL query to display only the empty and NULL values together?
- Empty string in not-null column in MySQL?
- MongoDB query to select one field if the other is null and the first field if both are not null?
- How it is possible to insert a zero or an empty string into a MySQL column which is defined as NOT NULL?
- MongoDB query to remove empty objects in an object-array?
- How to implement MongoDB $or query?
- MongoDB Query to combine AND & OR?

Advertisements