Để kiểm tra sự tồn tại của nhiều trường, hãy sử dụng $ being cùng với $ và. Hãy để chúng tôi tạo một bộ sưu tập với các tài liệu -
> db.demo475.insertOne({"StudentFirstName":"Chris","StudentAge":23});{
"acknowledged" : true,
"insertedId" : ObjectId("5e80c113b0f3fa88e2279088")
}
>
db.demo475.insertOne({"StudentFirstName":"Bob","StudentAge":21,"StudentCountryName":"US"});{
"acknowledged" : true,
"insertedId" : ObjectId("5e80c127b0f3fa88e2279089")
}
> db.demo475.insertOne({"StudentFirstName":"David","StudentAge":22});{
"acknowledged" : true,
"insertedId" : ObjectId("5e80c135b0f3fa88e227908a")
} 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.demo475.find();
Điều này sẽ tạo ra kết quả sau -
{ "_id" : ObjectId("5e80c113b0f3fa88e2279088"), "StudentFirstName" : "Chris", "StudentAge" :
23 }
{ "_id" : ObjectId("5e80c127b0f3fa88e2279089"), "StudentFirstName" : "Bob", "StudentAge" :
21, "StudentCountryName" : "US" }
{ "_id" : ObjectId("5e80c135b0f3fa88e227908a"), "StudentFirstName" : "David", "StudentAge"
: 22 } Sau đây là truy vấn để kiểm tra sự tồn tại của nhiều trường -
> db.demo475.find(
... { '$and':
... [ { 'StudentFirstName': { '$exists': true } },
... { 'StudentAge': { '$exists': true } },
... { 'StudentCountryName': { '$exists': true } } ]
... }
... ); Điều này sẽ tạo ra kết quả sau -
{ "_id" : ObjectId("5e80c127b0f3fa88e2279089"), "StudentFirstName" : "Bob", "StudentAge" :
21, "StudentCountryName" : "US" }