NumberInt () được sử dụng để chỉ định rõ ràng các số nguyên 32 bit. Hãy để chúng tôi tạo một bộ sưu tập với các tài liệu -
> db.demo357.insertOne(
... {
... "FirstName" : "Chris",
... "Age" : 21,
... "details" : {
... "studentDetails" : {
... "id" : NumberInt(101)
... }
... }
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5e568fa6f8647eb59e5620c9")
}
> db.demo357.insertOne(
... {
... "FirstName" : "David",
... "Age" : 23,
... "details" : {
... "studentDetails" : {
... "id" : NumberInt(110)
... }
... }
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5e568fbaf8647eb59e5620ca")
} 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.demo357.find();
Điều này sẽ tạo ra kết quả sau -
{ "_id" : ObjectId("5e568fa6f8647eb59e5620c9"), "FirstName" : "Chris", "Age" : 21, "details" : { "studentDetails" : { id" : 101 } } }
{ "_id" : ObjectId("5e568fbaf8647eb59e5620ca"), "FirstName" : "David", "Age" : 23, "details" : { "studentDetails" : { "id" : 110 } } } Đây là truy vấn để lấy tài liệu cụ thể -
> db.demo357.find({"details.studentDetails.id":NumberInt(110)}); Điều này sẽ tạo ra kết quả sau -
{ "_id" : ObjectId("5e568fbaf8647eb59e5620ca"), "FirstName" : "David", "Age" : 23, "details" : { "studentDetails" : { "id" : 110 } } }