Computer >> Máy Tính >  >> Lập trình >> MongoDB

Tìm nạp một tài liệu cụ thể trong MongoDB với các phần tử mảng

Để tìm nạp một tài liệu cụ thể, hãy sử dụng ký hiệu dấu chấm trong MongoDB find (). Hãy để chúng tôi tạo một bộ sưu tập với các tài liệu -

> db.demo672.insertOne({Brand:[{CategoryName:"Mobile","Name":"Oppo"}]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ea3ea9b04263e90dac943e5")
}
> db.demo672.insertOne({Brand:[{CategoryName:"Mobile","Name":"Samsung"}]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ea3eaa404263e90dac943e6")
}
> db.demo672.insertOne({Brand:[{CategoryName:"Mobile","Name":"OnePlus"}]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ea3eacc04263e90dac943e7")
}

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.demo672.find();

Điều này sẽ tạo ra kết quả sau -

{ "_id" : ObjectId("5ea3ea9b04263e90dac943e5"), "Brand" : [ { "CategoryName" : "Mobile", "Name" : "Oppo" } ] }
{ "_id" : ObjectId("5ea3eaa404263e90dac943e6"), "Brand" : [ { "CategoryName" : "Mobile", "Name" : "Samsung" } ] }
{ "_id" : ObjectId("5ea3eacc04263e90dac943e7"), "Brand" : [ { "CategoryName" : "Mobile", "Name" : "OnePlus" } ] }

Sau đây là truy vấn để tìm nạp một tài liệu cụ thể -

> db.demo672.find({"Brand.Name":"OnePlus"});

Điều này sẽ tạo ra kết quả sau -

{ "_id" : ObjectId("5ea3eacc04263e90dac943e7"), "Brand" : [ { "CategoryName" : "Mobile", "Name" : "OnePlus" } ] }