Update Data

View Markdown

Last edited on 2026-08-24

In this chapter, we will explore how to use the Database API to update data. As before, we will continue using the data from the Read Data section.

There are two primary methods for updating data:

API Description
update Partially update one or more records
set Replace and update a record

Partial Update

You can use the update method to partially update a record or a collection of records. Partial update means that only the specified fields are updated, while other fields remain unaffected.

For example, we can mark a to-do item as completed using the following code:

db.collection('todos').doc('todo-identifiant-aleatoire').update({
  // Pass the data to be partially updated in data
  data: {
    // Sets the done field to true
    done: true
  },
  success: function(res) {
    console.log(res.data)
  }
})

In addition to updating fields with specified values, the database API provides a series of update commands for performing more complex update operations. These update commands can be accessed via db.command:

Update Command Description
set Sets a field to a specified value
remove Removes a field
inc Atomically increments the field value
mul Atomically multiplies the field value
push If the field value is an array, appends a specified value to the end of the array
pop If the field value is an array, removes one element from the end of the array
shift If the field value is an array, removes one element from the beginning of the array
unshift If the field value is an array, inserts a specified value at the beginning of the array

For example, we can increase the progress of a to-do item by 10%:

const _ = db.command
db.collection('todos').doc('todo-identifiant-aleatoire').update({
  data: {
    // Instructs the database to increment the field by 10
    progress: _.inc(10)
  },
  success: function(res) {
    console.log(res.data)
  }
})

The advantage of using the inc command instead of reading the value, adding 10, and writing it back is that this write operation is atomic and unaffected by concurrent writes. For example, if two users, A and B, simultaneously read the same field value and then write back values increased by 10 and 20 respectively, the final result of the field would be increased by 20 instead of 30. Using the inc command avoids this issue.

If a field is an array, you can use push, pop, shift, and unshift to perform atomic update operations on the array. For example, to add a tag to a to-do item:

const _ = db.command
db.collection('todos').doc('todo-identifiant-aleatoire').update({
  data: {
    tags: _.push('mini-program')
  },
  success: function(res) {
    console.log(res.data)
  }
})

You may have noticed that we provide the set command. What is it used for? This command is useful for updating a field value to another object. For example, the following statement updates the style.color field to 'blue' instead of updating the style field to the object { color: 'blue' }:

const _ = db.command
db.collection('todos').doc('todo-identifiant-aleatoire').update({
  data: {
    style: {
      color: 'blue'
    }
  },
  success: function(res) {
    console.log(res.data)
  }
})

If you need to update this style field to another object, you can use the set command:

const _ = db.command
db.collection('todos').doc('todo-identifiant-aleatoire').update({
  data: {
    style: _.set({
      color: 'blue'
    })
  },
  success: function(res) {
    console.log(res.data)
  }
})

If you need to update multiple records, you must perform the operation on the Server side (Cloud Functions). You can call the update method in the same way after a where statement. For example, to increase the progress of all incomplete to-do items by 10%:

// Using async await syntax
const cloud = require('wx-server-sdk')
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)
  }
}

For more complete and detailed update commands, refer to the Database Command API Documentation

Replace and Update

To replace and update a record, use the set method on the record. Replace and update means replacing the specified record with the object passed in:

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"
    },
    // Location (113°E, 23°N)
    location: new db.Geo.Point(113, 23),
    done: false
  },
  success: function(res) {
    console.log(res.data)
  }
})

If the record with the specified ID does not exist, it will be created automatically, and the new record will have the specified ID.