# db.command.inc
Update command, indicating that the value of a field is automatically increased by the specified value. This is an atomic operation, and the advantage of using this operation command instead of reading data first, and then adding the data and writing it back is as follows:
- Atomicity: When multiple users write data to a field at the same time, the value of the field is automatically increased by 1 in the database, and new data does not overwrite previous data.
- One network request is reduced: There is no need to read the data first and then write it back.
Same as the mul command.
Function signature:
function inc(value: number): Command
Sample code
Automatically increase the progress of a todo by 10:
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const _ = db.command
exports.main = async (event, context) => {
try {
return await db.collection('todos').doc('todo-id').update({
data: {
progress: _.inc(10)
}
})
} catch(e) {
console.error(e)
}
}