Hướng dẫn inc mongodb
Docs Home → MongoDB Manual Show $inc The
To specify a Starting in MongoDB 5.0, update operators process document fields with string-based names in lexicographic order. Fields with numeric names are processed in numeric order. See Update Operators Behavior for details. The If the field does not exist, Use of the
Starting in MongoDB 5.0, Create the
The following
The updated document would resemble:
Tip
Definition¶$inc ¶The { $inc: { <field1>: <amount1>, <field2>: <amount2>, ... } } To specify a Behavior¶The If the field does not exist,
Use of the
Example¶Consider a collection { _id: 1, sku: "abc123", quantity: 10, metrics: { orders: 2, ratings: 3.5 } } The following
db.products.update( { sku: "abc123" }, { $inc: { quantity: -2, "metrics.orders": 1 } } ) The updated document would resemble: { "_id" : 1, "sku" : "abc123", "quantity" : 8, "metrics" : { "orders" : 3, "ratings" : 3.5 } } |