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

WeChat Small Program Cloud Development 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

Options are required parameters and are an object in the following format, such as incoming success, fail, complete, which means that the callback style is used and Promise is not returned.

The field name Type Required The default Description
data Object Is Update the object
success Function Whether Successful callbacks, callbacks to incoming parameters Result contain the results of the query, the Result definition is below
fail Function Whether Failed callback
complete Function Whether Callback function at the end of the call (both successful and failed)

Returns a description of the value

If the incoming options parameter does not have a success, fail, complete field, a Promise is returned, otherwise no value is returned. Promise's resolve and reject results 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

Sample code

Update the premature item, add all unsatisfactory progress to 10:

Callback style

const _ = db.command
db.collection('todos').where({
  done: false  
}).update({
  data: {
    progress: _.inc(10)
  },
  success: console.log,
  fail: console.error
})

Promise style

const _ = db.command
db.collection('todos').where({
  done: false  
})
.update({
  data: {
    progress: _.inc(10)
  },
})
.then(console.log)
.catch(console.error)