
- 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
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 Articles
- Handling Empty Cells using CSS
- Overflow Handling in Data Structure
- What is data handling?
- What is meant by data and data handling?
- Removing empty fields from MongoDB
- Check if a list is not empty in MongoDB?
- How to check empty field in a MongoDB collection?
- Updating data in MongoDB
- What is data handling? What are the different ways to represent data?
- Optional chaining operator in JavaScript.
- Optional Parameters in Dart Programming
- MongoDB query to remove empty objects in an object-array?
- How does missing data handling make selection bias worse?
- Delete partial data in MongoDB?
- How to create an empty data frame in R?

Advertisements