먼저 문서로 컬렉션을 만들어 보겠습니다. −
>db.demo11.insertOne({"ListOfStudent":[{"StudentName":"Chris","ListOfScore":[76,67,54,89]}]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e0f6e34d7df943a7cec4fa1")
} 다음은 find() 메서드를 사용하여 컬렉션의 모든 문서를 표시하는 쿼리입니다. -
> db.demo11.find().pretty();
이것은 다음과 같은 출력을 생성합니다 -
{
"_id" : ObjectId("5e0f6e34d7df943a7cec4fa1"),
"ListOfStudent" : [
{
"StudentName" : "Chris",
"ListOfScore" : [
76,
67,
54,
89
]
}
]
} 다음은 조건이 −
인 배열 요소를 삽입하는 쿼리입니다.> db.demo11.update( {"ListOfStudent.StudentName":"Chris"}, {$push:{"ListOfStudent.$.ListOfScore":98}} );
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) 문서를 다시 한 번 확인합시다 -
> db.demo11.find().pretty();
이것은 다음과 같은 출력을 생성합니다 -
{
"_id" : ObjectId("5e0f6e34d7df943a7cec4fa1"),
"ListOfStudent" : [
{
"StudentName" : "Chris",
"ListOfScore" : [
76,
67,
54,
89,
98
]
}
]
}