更新指令。用于设定字段等于指定值。
函数签名:
function set(value: any): Command
这种方法相比传入纯 JS 对象的好处是能够指定字段等于一个对象:
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
exports.main = async (event, context) => {
  try {
    // 以下方法只会更新 style.color 为 red,而不是将 style 更新为 { color: 'red' },即不影响 style 中的其他字段
    const res1 = await db.collection('todos').doc('doc-id').update({
      data: {
        style: {
          color: 'red'
        }
      }
    })
    // 以下方法更新 style 为 { color: 'red', size: 'large' }
    const res2 = await db.collection('todos').doc('doc-id').update({
      data: {
        style: _.set({
          color: 'red',
          size: 'large'
        })
      }
    })
    return {
      res1,
      res2,
    }
  } catch(e) {
    console.error(e)
  }
}更新指令。用于表示删除某个字段。
函数签名:
function remove(): Command
示例代码
删除 style 字段:
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-id').update({
      data: {
        style: _.remove()
      }
    })
  } catch(e) {
    console.error(e)
  }
}更新指令。用于指示字段自增某个值,这是个原子操作,使用这个操作指令而不是先读数据、再加、再写回的好处是:
mul 指令同理。
函数签名:
function inc(value: number): Command
示例代码
将一个 todo 的进度自增 10:
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-id').update({
      data: {
        progress: _.inc(10)
      }
    })
  } catch(e) {
    console.error(e)
  }
}更新指令。用于指示字段自乘某个值,这是个原子操作,使用这个操作指令而不是先读数据、再加、再写回的好处是:
inc 指令同理。
函数签名:
function mul(value: number): Command
示例代码
将一个 todo 的进度乘 2:
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-id').update({
      data: {
        progress: _.mul(2)
      }
    })
  } catch(e) {
    console.error(e)
  }
}更新指令,对一个值为数组的字段,往数组尾部添加一个或多个值。或字段原为空,则创建该字段并设数组为传入值。
函数签名:
function push(values: any[]): Command
示例代码
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('doc-id').update({
      data: {
        tags: _.push(['mini-program', 'cloud'])
      }
    })
  } catch(e) {
    console.error(e)
  }
}更新指令,对一个值为数组的字段,将数组尾部元素删除。
函数签名:
function pop(values: any[]): Command
示例代码
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('doc-id').update({
      data: {
        tags: _.pop()
      }
    })
  } catch(e) {
    console.error(e)
  }
}更新指令,对一个值为数组的字段,将数组头部元素删除。
函数签名:
function shift(values: any[]): Command
示例代码
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('doc-id').update({
      data: {
        tags: _.shift()
      }
    })
  } catch(e) {
    console.error(e)
  }
}更新指令,对一个值为数组的字段,往数组头部添加一个或多个值。或字段原为空,则创建该字段并设数组为传入值。
函数签名:
function unshift(values: any[]): Command
示例代码
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('doc-id').update({
      data: {
        tags: _.unshift(['mini-program', 'cloud'])
      }
    })
  } catch(e) {
    console.error(e)
  }
}(c) 2024 chaojicainiao.com MIT license