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

WeChat small program cloud development API update instructions


May 20, 2021 WeChat Mini Program Development Document


Table of contents


db.command.set

Update the instruction. Used to set the field equal to the specified value.

Function signature:

function set(value: any): Command

The advantage of this approach over passing in pure JS objects is the ability to specify that a field is equal to one object:

// 以下方法只会更新 style.color 为 red,而不是将 style 更新为 { color: 'red' },即不影响 style 中的其他字段
db.collection('todos').doc('doc-id').update({
  data: {
    style: {
      color: 'red'
    }
  }
})

// 以下方法更新 style 为 { color: 'red', size: 'large' }
db.collection('todos').doc('doc-id').update({
  data: {
    style: _.set({
      color: 'red',
      size: 'large'
    })
  }
})

db.command.remove

Update the instruction. Used to indicate the deletion of a field.

Function signature:

function remove(): Command

The sample code

Remove the style field:

const _ = db.command
db.collection('todos').doc('todo-id').update({
  data: {
    style: _.remove()
  }
})

db.command.inc

Update the instruction. Used to indicate that the field adds a value, which is an atomic operation, and the advantage of using this instruction instead of reading the data first, then adding it, and then writing it back is:

  1. Atomicity: Multiple users write at the same time, for the database is to add fields to one, there will be no later override of the former situation
  2. Reduce a network request: you don't need to read and write

The mul directive is the same.

Function signature:

function inc(value: number): Command

The sample code

Increase the progress of a todo by 10:

const _ = db.command
db.collection('todos').doc('todo-id').update({
  data: {
    progress: _.inc(10)
  }
})

db.command.mul

Update the instruction. Used to indicate that a field multiplies a value, which is an atomic operation, and the advantage of using this instruction instead of reading the data first, then adding, and then writing it back is:

  1. Atomicity: Multiple users write at the same time, for the database are self-multiplied fields, there will be no later override of the former situation
  2. Reduce a network request: you don't need to read and write

The inc directive is the same.

Function signature:

function mul(value: number): Command

The sample code

Multiply the progress of a todo by 2:

const _ = db.command
db.collection('todos').doc('todo-id').update({
  data: {
    progress: _.mul(2)
  }
})

db.command.push

Update the instruction to add one or more values to the end of the array to a field with an array of values. Or if the field was originally empty, the field is created and an array is set as an incoming value.

Function signature:

function push(values: any[]): Command

The sample code

const _ = db.command
db.collection('todos').doc('doc-id').update({
  data: {
    tags: _.push(['mini-program', 'cloud'])
  }
})

db.command.pop

Update the instruction to remove the tail element of the array from a field with an array value.

Function signature:

function pop(values: any[]): Command

The sample code

const _ = db.command
db.collection('todos').doc('doc-id').update({
  data: {
    tags: _.pop()
  }
})

db.command.shift

Update the instruction to remove the array header element from a field with an array value.

Function signature:

function shift(values: any[]): Command

The sample code

const _ = db.command
db.collection('todos').doc('doc-id').update({
  data: {
    tags: _.shift()
  }
})

db.command.unshift

Update the instruction to add one or more values to the head of the array to a field with an array value. Or if the field was originally empty, the field is created and an array is set as an incoming value.

Function signature:

function unshift(values: any[]): Command

The sample code

const _ = db.command
db.collection('todos').doc('doc-id').update({
  data: {
    tags: _.unshift(['mini-program', 'cloud'])
  }
})