-
Notifications
You must be signed in to change notification settings - Fork 84
Update Operators: $inc
o5faruk edited this page May 2, 2021
·
4 revisions
This sdk documentation is deprecated and will not be updated. Check out our new docs at https://sdk.buildfire.com/docs/
$inc
The $inc operator increments a field by a specified value and has the following form:
{ $inc: { <field1>: <amount1>} }
To specify a in an embedded document or in an array, use dot notation.
The $inc operator accepts positive and negative values.
If the field does not exist, $inc creates the field and sets the field to the specified value.
Use of the $inc operator on a field with a null value will generate an error.
$inc is an atomic operation within a single document.
Consider a collection products with the following document:
{
id: 123,
name: "Corn Flakes",
inventory: 5,
metrics: {
minQty: 2,
reOrderQty: 10,
totalSold: 55
}
}
The following update() operation uses the $inc operator to decrease the quantity field by 1 (i.e. increase by -1) and increase the "metrics.totalSold" field by 1:
buildfire.datastore.update(
{ id: 123 },
{ $inc: { inventory: -1 } ,$inc: {"metrics.totalSold": 1} }
);
The updated document would resemble:
{
id: 123,
name: "Corn Flakes",
inventory: 4,
metrics: {
minQty: 2,
reOrderQty: 10,
totalSold: 56
}
}