

- 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
Handling optional/empty data in MongoDB?
In order to handle empty data, you can use $ne operator. Let us create a collection with documents. Following is the query
>db.handlingAndEmptyDataDemo.insertOne({"StudentName":"John","StudentCountryName":""}); { "acknowledged" : true, "insertedId" : ObjectId("5c9cbd5ca629b87623db1b12") } >db.handlingAndEmptyDataDemo.insertOne({"StudentName":"John","StudentCountryName":null}); { "acknowledged" : true, "insertedId" : ObjectId("5c9cbd6ba629b87623db1b13") } > db.handlingAndEmptyDataDemo.insertOne({"StudentName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9cbd71a629b87623db1b14") }
Following is the query to display all documents from a collection with the help of find() method
> db.handlingAndEmptyDataDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9cbd5ca629b87623db1b12"), "StudentName" : "John", "StudentCountryName" : "" } { "_id" : ObjectId("5c9cbd6ba629b87623db1b13"), "StudentName" : "John", "StudentCountryName" : null } { "_id" : ObjectId("5c9cbd71a629b87623db1b14"), "StudentName" : "John" }
Following is the query to handle empty data using $ne
> db.handlingAndEmptyDataDemo.find({StudentCountryName: {$ne: null}});
This will produce the following output
{ "_id" : ObjectId("5c9cbd5ca629b87623db1b12"), "StudentName" : "John", "StudentCountryName" : "" }
- Related Questions & Answers
- Handling Empty Cells using CSS
- Overflow Handling in Data Structure
- Removing empty fields from MongoDB
- Optional chaining operator in JavaScript.
- Optional Parameters in Dart Programming
- Mandatory and optional Participation
- Java 8 Optional Class
- Updating data in MongoDB
- Optional property in a C# class
- Explain Optional Catch Binding in JavaScript.
- Check if a list is not empty in MongoDB?
- How to check empty field in a MongoDB collection?
- Role of CSS :optional Selector
- MongoDB query to remove empty objects in an object-array?
- Delete partial data in MongoDB?
Advertisements