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

Làm cách nào để lấy dữ liệu cụ thể ở các định dạng khác nhau với MongoDB?

Đối với điều này, chỉ cần sử dụng find (). Đối với một định dạng khác, hãy sử dụng khá (). Trước tiên, hãy để chúng tôi tạo một bộ sưu tập với các tài liệu -

> db.getSpecificData.insertOne(
... {
...    "StudentName": "John",
...    "Information": {
...       "FatherName": "Chris",
...       "Place": {
...          "CountryName": "US",
...          "ZipCode":"111344"
...       },
...       "id": "1"
...    }
... }
... );
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e039abdf5e889d7a5199509")
}
> db.getSpecificData.insertOne(
...    {
...       "StudentName": "Carol",
...       "Information": {
...          "FatherName": "Robert",
...          "Place": {
...             "CountryName": "UK",
...             "ZipCode":"746464"
...          },
...          "id": "2"
...       }
...    }
... );
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e039ae6f5e889d7a519950a")
}
>
> db.getSpecificData.insertOne(
... {
...    "StudentName": "David",
...    "Information": {
...       "FatherName": "Carol",
...       "Place": {
...          "CountryName": "US",
...          "ZipCode":"567334"
...       },
...    "id": "3"
...    }
... }
... );
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e039ae7f5e889d7a519950b")
}
>
> db.getSpecificData.insertOne(
...    {
...       "StudentName": "Jace",
...       "Information": {
...          "FatherName": "Bob",
...          "Place": {
...             "CountryName": "US",
...             "ZipCode":"999999"
...          },
...          "id": "4"
...       }
...    }
... );
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e039ae8f5e889d7a519950c")
}

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.getSpecificData.find({'Information.Place.CountryName':"US"}, {}, {limit: 2}, function(error, data) {}).pretty();

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

{
   "_id" : ObjectId("5e039abdf5e889d7a5199509"),
   "StudentName" : "John",
   "Information" : {
      "FatherName" : "Chris",
      "Place" : {
         "CountryName" : "US",
         "ZipCode" : "111344"
      },
   "id" : "1"
   }
}
{
   "_id" : ObjectId("5e039ae7f5e889d7a519950b"),
   "StudentName" : "David",
   "Information" : {
      "FatherName" : "Carol",
      "Place" : {
         "CountryName" : "US",
         "ZipCode" : "567334"
      },
   "id" : "3"
   }
}
{
   "_id" : ObjectId("5e039ae8f5e889d7a519950c"),
   "StudentName" : "Jace",
   "Information" : {
      "FatherName" : "Bob",
      "Place" : {
         "CountryName" : "US",
         "ZipCode" : "999999"
      },
   "id" : "4"
   }
}

Sau đây là truy vấn để lấy dữ liệu cụ thể ở định dạng khác -

> db.getSpecificData.find({'Information.Place.CountryName':"US"}, {}).limit(2);

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

{ "_id" : ObjectId("5e039abdf5e889d7a5199509"), "StudentName" : "John", "Information" : { "FatherName" : "Chris", "Place" : { "CountryName" : "US", "ZipCode" : "111344" }, "id" : "1" } }
{ "_id" : ObjectId("5e039ae7f5e889d7a519950b"), "StudentName" : "David", "Information" : { "FatherName" : "Carol", "Place" : { "CountryName" : "US", "ZipCode" : "567334" }, "id" : "3" } }