# Document.set

Replaces and updates a record.

Function signature is shown as below:

function set(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

Result from Promise resolve is an object with the following structure:

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

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"
        },
        // Location (113°E, 23°N)
        location: new db.Geo.Point(113, 23),
        done: false
      }
    })
  } catch(e) {
    console.error(e)
  }
}