여기에서 StudentDetails 내부에 있는 MongoDB에 대한 StudentScores를 증가시킨다고 가정해 보겠습니다. -
... "StudentScores": {
... "StudentMathScore": 90,
... "StudentMongoDBScore": 78
... } 먼저 문서로 컬렉션을 만들어 보겠습니다. −
> db.embeddedValueIncrementDemo.insertOne(
... {
... "StudentDetails": {
... "StudentScores": {
... "StudentMathScore": 90,
... "StudentMongoDBScore": 78
... }
... }
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd2b670345990cee87fd896")
} 다음은 find() 메서드를 사용하여 컬렉션의 모든 문서를 표시하는 쿼리입니다. -
> db.embeddedValueIncrementDemo.find().pretty();
이것은 다음과 같은 출력을 생성합니다 -
{
"_id" : ObjectId("5cd2b670345990cee87fd896"),
"StudentDetails" : {
"StudentScores" : {
"StudentMathScore" : 90,
"StudentMongoDBScore" : 78
}
}
} 다음은 임베디드 값을 증가시키는 쿼리입니다. 여기에서 StudentMongoDBScore를 증가시킵니다 -
> db.embeddedValueIncrementDemo.update({ _id: new ObjectId("5cd2b670345990cee87fd896") }, { $inc: { "StudentDetails.StudentScores.StudentMongoDBScore": 20 } }, { upsert: true, safe: true }, null);
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) 모든 문서를 다시 한 번 확인합시다 -
> db.embeddedValueIncrementDemo.find().pretty();
이것은 다음과 같은 출력을 생성합니다 -
{
"_id" : ObjectId("5cd2b670345990cee87fd896"),
"StudentDetails" : {
"StudentScores" : {
"StudentMathScore" : 90,
"StudentMongoDBScore" : 98
}
}
}