
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Ignore NULL and UNDEFINED values while running a MongoDB query
To ignore NULL and UNDEFINED values, use $ne in MongoDB. Let us create a collection with documents −
> db.demo35.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e175e42cfb11e5c34d898d0") } > db.demo35.insertOne({"Name":null}); { "acknowledged" : true,9 "insertedId" : ObjectId("5e175e46cfb11e5c34d898d1") } > db.demo35.insertOne({"Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e175e4bcfb11e5c34d898d2") } > db.demo35.insertOne({"Name":undefined}); { "acknowledged" : true, "insertedId" : ObjectId("5e175e54cfb11e5c34d898d3") }
Display all documents from a collection with the help of find() method −
> db.demo35.find();
This will produce the following output −
{ "_id" : ObjectId("5e175e42cfb11e5c34d898d0"), "Name" : "Chris" } { "_id" : ObjectId("5e175e46cfb11e5c34d898d1"), "Name" : null } { "_id" : ObjectId("5e175e4bcfb11e5c34d898d2"), "Name" : "Bob" } { "_id" : ObjectId("5e175e54cfb11e5c34d898d3"), "Name" : undefined }
Following is the query to use $new to ignore NULL values −
> db.demo35.find({"Name": {$ne: null}});
This will produce the following output −
{ "_id" : ObjectId("5e175e42cfb11e5c34d898d0"), "Name" : "Chris" } { "_id" : ObjectId("5e175e4bcfb11e5c34d898d2"), "Name" : "Bob" }
- Related Questions & Answers
- Ignore null values in MongoDB documents
- Ignore NULL values from separate tables in a single MySQL query and display count of NOT NULL records
- Ignore null values in MySQL and display rest of the values
- Execution time while running a SQL query in HANA Studio
- Count zero, NULL and distinct values except zero and NULL with a single MySQL query
- Ignore first 4 values in MongoDB documents and display the next 3?
- How to check for 'undefined' or 'null' in a JavaScript array and display only non-null values?
- MySQL query to order by NULL values
- How to handle Null values while working with JDBC?
- Replace a value if null or undefined in JavaScript?
- What is the difference between null and undefined in JavaScript?
- Display the undefined and exact MongoDB document records
- Query non-empty values of a row first in ascending order and then display NULL values
- MySQL query to display only the empty and NULL values together?
- How to split MongoDB query and skip 5 values?
Advertisements