Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MongoDB

ลดค่าลง 1 รายการใน MongoDB ด้วย $inc และเงื่อนไข $gt

บทความนี้สาธิตการใช้ $inc (Increment) ด้วยค่าลบ เพื่อ Decrement ค่าของฟิลด์ตัวเลขในเอกสารเฉพาะเจาะจง พร้อมเงื่อนไข $gt ป้องกันค่าติดลบ

สร้างข้อมูลตัวอย่าง

> 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") }

แสดงข้อมูลก่อน Decrement

> 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 }

ลดราคา Product-4 ลง 10 (ถ้าราคา > 0)

> db.decrementingOperationDemo.update(
  { _id: ObjectId("5cd7a8d06d78f205348bc63f"), ProductPrice: { $gt: 0 } },
  { $inc: { ProductPrice: -10 } }
);
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

ตรวจสอบผลลัพธ์ (Product-4: 1000 → 990)

> 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 }

คำอธิบาย

  • $inc: { ProductPrice: -10 } - เพิ่ม -10 (คือ ลด 10) ให้กับ ProductPrice
  • ProductPrice: { $gt: 0 } ใน Query - Conditional Update อัปเดตก็ต่อเมื่อราคา > 0 (ป้องกันราคาติดลบ)
  • $inc เป็น Atomic Operation - ปลอดภัยสำหรับ Concurrent Updates

ตัวอย่างเพิ่มเติม: Decrement หลายฟิลด์พร้อมกัน

db.inventory.updateOne(
  { _id: ObjectId("..."), quantity: { $gt: 0 } },
  { $inc: { quantity: -1, reserved: 1 } }
);

ตัวอย่าง: ใช้กับ Array Element (Positional $)

// ลด Stock ของ Product ใน Order Array
db.orders.updateOne(
  { "items.productId": "P001", "items.$.quantity": { $gt: 0 } },
  { $inc: { "items.$.quantity": -1 } }
);

Operator คำนวณตัวเลขอื่นๆ

  • $inc - เพิ่ม/ลด (รองรับ Negative)
  • $mul - คูณ (เช่น ลด 10%: { $mul: { price: 0.9 } })
  • $min, $max - ตั้งค่า Min/Max (อัปเดตก็ต่อเมื่อค่าใหม่ น้อย/มากกว่าเดิม)