Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat small program cloud development service-side database API updates multiple records


May 20, 2021 WeChat Mini Program Development Document


Table of contents


Collection.update / Query.update

Update multiple records

The function signature is as follows:

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

Description of the parameters

The field name Type Required The default Description
data Object Is Update the object

Returns a description of the value

The results of Promise's resolve and reject are defined as follows:

Description of the results
resolve For the results of the new record, the Result definition is below
reject The reason for the failure

Result description

The result of the success callback and the result of Promise Resolve result are objects that are structured as follows:

Field Type Description
stats Object Update the statistics for the results, which contain fields that see the definition of stats below

The stats object is an object that is structured as follows:

Field Type Description
updated number The number of records that were successfully updated
Note: The success of an API call does not necessarily mean that the record you want to update has been updated, for example, it is possible to specify that the where filter can only filter out 0 matching records, so you get a situation where the update API call was successful but no records were actually updated, as can be seen by stats.updated

The sample code

Update to-dos to add 10 to-do progress to all outstanding 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)
  }
}