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

WeChat small program cloud development 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

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
_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

Sample code

const _ = db.command
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
  },
  success: function(res) {
    console.log(res.data)
  }
})