중첩된 JSON 배열에서 값을 검색하려면 아래 구문을 사용할 수 있습니다 -
db.yourCollectionName.find({"yourOuterFieldName.yourInnerFieldName.yourNextInnerFieldName…...N": "yourValue"}).pretty(); 먼저 문서로 컬렉션을 생성해 보겠습니다. −
> db.nestedJSONArrayDemo.insertOne({
... "ClientDetails" :
... {
... "ClientPersonalDetails" : [
... { "CountryName" : "US" },
... { "CountryName" : "AUS"},
... { "ClientName":"Chris" },
... { "ClientName":"David" }
... ]
... }
... });
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd2cbb2b64f4b851c3a13bc")
}
> db.nestedJSONArrayDemo.insertOne({
... "ClientDetails" :
... {
... "ClientPersonalDetails" : [
... { "CountryName" : "Belgium" },
... { "CountryName" : "Canada"},
... { "CountryName" : "Egypt"},
... ]
... }
... });
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd2cc14b64f4b851c3a13bd")
} 다음은 find() 메서드를 사용하여 컬렉션의 모든 문서를 표시하는 쿼리입니다. -
> db.nestedJSONArrayDemo.find().pretty();
이것은 다음과 같은 출력을 생성합니다 -
{
"_id" : ObjectId("5cd2cbb2b64f4b851c3a13bc"),
"ClientDetails" : {
"ClientPersonalDetails" : [
{
"CountryName" : "US"
},
{
"CountryName" : "AUS"
},
{
"ClientName" : "Chris"
},
{
"ClientName" : "David"
}
]
}
}
{
"_id" : ObjectId("5cd2cc14b64f4b851c3a13bd"),
"ClientDetails" : {
"ClientPersonalDetails" : [
{
"CountryName" : "Belgium"
},
{
"CountryName" : "Canada"
},
{
"CountryName" : "Egypt"
}
]
}
} 다음은 MongoDB의 중첩된 JSON 배열에서 값을 검색하는 쿼리입니다 -
> db.nestedJSONArrayDemo.find({"ClientDetails.ClientPersonalDetails.CountryName": "Canada"}).pretty(); 이것은 다음과 같은 출력을 생성합니다 -
{
"_id" : ObjectId("5cd2cc14b64f4b851c3a13bd"),
"ClientDetails" : {
"ClientPersonalDetails" : [
{
"CountryName" : "Belgium"
},
{
"CountryName" : "Canada"
},
{
"CountryName" : "Egypt"
}
]
}
}