Để xử lý dữ liệu trống, bạn có thể sử dụng toán tử $ ne. Hãy để chúng tôi tạo một bộ sưu tập với các tài liệu. Sau đây là truy vấn
>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")
} Sau đây là truy vấn để hiển thị tất cả các tài liệu từ một bộ sưu tập với sự trợ giúp của phương thức find ()
> db.handlingAndEmptyDataDemo.find().pretty();
Điều này sẽ tạo ra kết quả sau
{
"_id" : ObjectId("5c9cbd5ca629b87623db1b12"),
"StudentName" : "John",
"StudentCountryName" : ""
}
{
"_id" : ObjectId("5c9cbd6ba629b87623db1b13"),
"StudentName" : "John",
"StudentCountryName" : null
}
{ "_id" : ObjectId("5c9cbd71a629b87623db1b14"), "StudentName" : "John" } Sau đây là truy vấn để xử lý dữ liệu trống bằng cách sử dụng $ ne
> db.handlingAndEmptyDataDemo.find({StudentCountryName: {$ne: null}}); Điều này sẽ tạo ra kết quả sau
{ "_id" : ObjectId("5c9cbd5ca629b87623db1b12"), "StudentName" : "John", "StudentCountryName" : "" }