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" : "" }