# Collection.update / Query.update

Updates multiple records.

Function signature is shown as below:

function update(options: object): Promise<Result>

Parameter description

Field Type Required Default Description
data Object Yes Updates objects

Description of return value

The results of resolve and reject for Promise are defined as below:

Result Description
resolve The result of a new record. The Result is defined as below.
reject Reason for failure

Result description

The result of success callback and Result of resolve for Promise are objects with the following structure:

Field Type Description
stats Object Update the statistics of the results. Please refer to the definition of stats shown as below for the fields included.

The stats object is an object with the following structure:

Field Type Description
updated number Number of records updated successfully

Note: The success of API call does not necessarily mean that the record you want to update has been updated. For example, the specified filter condition "where" can only filter out 0 matched record, so you may see that the call of updated API is done successfully but no record is updated. In this case, use stats.updated to check the update status.

Sample code

Update to-dos by adding 10 to all unfinished to-dos:

Promise style

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').where({
      done: false
    })
    .update({
      data: {
        progress: _.inc(10)
      },
    })
  } catch(e) {
    console.error(e)
  }
}