먼저 문서로 컬렉션을 만들어 보겠습니다. −
>db.decrementingOperationDemo.insertOne({"ProductName":"Product-1","ProductPrice":756}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7a8ae6d78f205348bc63c") } >db.decrementingOperationDemo.insertOne({"ProductName":"Product-2","ProductPrice":890}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7a8b86d78f205348bc63d") } >db.decrementingOperationDemo.insertOne({"ProductName":"Product-3","ProductPrice":994}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7a8c66d78f205348bc63e") } >db.decrementingOperationDemo.insertOne({"ProductName":"Product-4","ProductPrice":1000}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7a8d06d78f205348bc63f") }
다음은 find() 메서드를 사용하여 컬렉션의 모든 문서를 표시하는 쿼리입니다. -
> db.decrementingOperationDemo.find().pretty();
이것은 다음과 같은 출력을 생성합니다 -
{ "_id" : ObjectId("5cd7a8ae6d78f205348bc63c"), "ProductName" : "Product-1", "ProductPrice" : 756 } { "_id" : ObjectId("5cd7a8b86d78f205348bc63d"), "ProductName" : "Product-2", "ProductPrice" : 890 } { "_id" : ObjectId("5cd7a8c66d78f205348bc63e"), "ProductName" : "Product-3", "ProductPrice" : 994 } { "_id" : ObjectId("5cd7a8d06d78f205348bc63f"), "ProductName" : "Product-4", "ProductPrice" : 1000 }
다음은 단일 값을 감소시키는 쿼리입니다 -
> db.decrementingOperationDemo.update({_id: ObjectId("5cd7a8d06d78f205348bc63f"), ProductPrice: {$gt: 0}}, {$inc: {ProductPrice: -10}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
문서를 다시 한 번 확인합시다 -
> db.decrementingOperationDemo.find().pretty();
이것은 다음과 같은 출력을 생성합니다 -
{ "_id" : ObjectId("5cd7a8ae6d78f205348bc63c"), "ProductName" : "Product-1", "ProductPrice" : 756 } { "_id" : ObjectId("5cd7a8b86d78f205348bc63d"), "ProductName" : "Product-2", "ProductPrice" : 890 } { "_id" : ObjectId("5cd7a8c66d78f205348bc63e"), "ProductName" : "Product-3", "ProductPrice" : 994 } { "_id" : ObjectId("5cd7a8d06d78f205348bc63f"), "ProductName" : "Product-4", "ProductPrice" : 990 }