Computer >> 컴퓨터 >  >> 프로그램 작성 >> MongoDB

MongoDB로 중첩 문자열의 쿼리 배열?

<시간/>

중첩된 문자열의 배열을 쿼리하려면 점(.) 표기법을 사용할 수 있습니다. 먼저 문서로 컬렉션을 만들어 보겠습니다. −

> db.nestedStringDemo.insertOne(
   {
      "CustomerName": "John",
      "CustomerOtherDetails": [ { "Age":29, "CountryName": "US" },
      { "CompanyName": "Amazon",
      "Salary": 150000, "ProjectName": ["Online Library Management System", "Pig Dice Game"]
   } ] }
);
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5cea4629ef71edecf6a1f690")
}
> db.nestedStringDemo.insertOne(
{
   "CustomerName": "Chris",
   "CustomerOtherDetails": [ { "Age":27, "CountryName": "AUS" },
   { "CompanyName": "Google",
      "Salary": 250000, "ProjectName": ["Chat Application", "Game Design"]
   } ] }
);
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5cea466eef71edecf6a1f691")
}

다음은 find() 메서드를 사용하여 컬렉션의 모든 문서를 표시하는 쿼리입니다. -

> db.nestedStringDemo.find().pretty();

이것은 다음과 같은 출력을 생성합니다 -

{
   "_id" : ObjectId("5cea4629ef71edecf6a1f690"),
   "CustomerName" : "John",
   "CustomerOtherDetails" : [
      {
         "Age" : 29,
         "CountryName" : "US"
      },
      {
         "CompanyName" : "Amazon",
         "Salary" : 150000,
         "ProjectName" : [
            "Online Library Management System",
            "Pig Dice Game"
         ]
      }
   ]
}
{
   "_id" : ObjectId("5cea466eef71edecf6a1f691"),
   "CustomerName" : "Chris",
   "CustomerOtherDetails" : [
      {
         "Age" : 27,
         "CountryName" : "AUS"
      },
      {
         "CompanyName" : "Google",
         "Salary" : 250000,
         "ProjectName" : [
            "Chat Application",
            "Game Design"
         ]
      }
   ]
}

이제 점 표기법을 사용하여 중첩된 문자열 배열을 쿼리해 보겠습니다. -

> db.nestedStringDemo.find({"CustomerOtherDetails.ProjectName":"Chat Application"}).pretty();

이것은 다음과 같은 출력을 생성합니다 -

{
   "_id" : ObjectId("5cea466eef71edecf6a1f691"),
   "CustomerName" : "Chris",
   "CustomerOtherDetails" : [
      {
         "Age" : 27,
         "CountryName" : "AUS"
      },
      {
         "CompanyName" : "Google",
         "Salary" : 250000,
         "ProjectName" : [
            "Chat Application",
            "Game Design"
         ]
      }
   ]
}