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

WeChat small program cloud development service-side database API replacement update a record


May 20, 2021 WeChat Mini Program Development Document


Table of contents


Document.set

Replace and update a record

The function signature is as follows:

function set(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 Promise Resolve is an object with the following structure:

Field Type Description
_id String | Number The ID of the record
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, _id if the specified number already exists, is 1, otherwise it is 0
created number The number of records that were successfully updated, _id 0 if the specified number already exists, otherwise it is 1

The sample code

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-identifiant-aleatoire').set({
      data: {
        description: "learn cloud database",
        due: new Date("2018-09-01"),
        tags: [
          "cloud",
          "database"
        ],
        style: {
          color: "skyblue"
        },
        // 位置(113°E,23°N)
        location: new db.Geo.Point(113, 23),
        done: false
      }
    })
  } catch(e) {
    console.error(e)
  }
}