하위 데이터에 도달하려면 MongoDB에서 키를 사용해야 합니다. 문서로 컬렉션을 만들자 −
>db.demo450.insertOne({"Information":{"StudentDetails":{"StudentName":"Chris","StudentAge":21}}}); {
"acknowledged" : true,
"insertedId" : ObjectId("5e7b590e71f552a0ebb0a6e6")
}
>db.demo450.insertOne({"Information":{"StudentDetails":{"StudentName":"David","StudentAge":23}}});{
"acknowledged" : true,
"insertedId" : ObjectId("5e7b591a71f552a0ebb0a6e7")
}
>db.demo450.insertOne({"Information":{"StudentDetails":{"StudentName":"Mike","StudentAge":22}}});{
"acknowledged" : true,
"insertedId" : ObjectId("5e7b592271f552a0ebb0a6e8")
} find() 메서드를 사용하여 컬렉션의 모든 문서 표시 -
> db.demo450.find();
이것은 다음과 같은 출력을 생성합니다 -
{ "_id" : ObjectId("5e7b590e71f552a0ebb0a6e6"), "Information" : { "StudentDetails" : {
"StudentName" : "Chris", "StudentAge" : 21 } } }
{ "_id" : ObjectId("5e7b591a71f552a0ebb0a6e7"), "Information" : { "StudentDetails" : {
"StudentName" : "David", "StudentAge" : 23 } } }
{ "_id" : ObjectId("5e7b592271f552a0ebb0a6e8"), "Information" : { "StudentDetails" : {
"StudentName" : "Mike", "StudentAge" : 22 } } } 다음은 MongoDB에서 하위 데이터에 도달하는 쿼리입니다 -
> db.demo450.find({"Information.StudentDetails.StudentName":"David"}); 이것은 다음과 같은 출력을 생성합니다 -
{ "_id" : ObjectId("5e7b591a71f552a0ebb0a6e7"), "Information" : { "StudentDetails" : {
"StudentName" : "David", "StudentAge" : 23 } } }