codecamp

微信小程序云开发服务端数据库API 更新指令

db.command.set

更新指令。用于设定字段等于指定值。

函数签名:

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)
  }
}

db.command.remove

更新指令。用于表示删除某个字段。

函数签名:

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)
  }
}

db.command.inc

更新指令。用于指示字段自增某个值,这是个原子操作,使用这个操作指令而不是先读数据、再加、再写回的好处是:

  1. 原子性:多个用户同时写,对数据库来说都是将字段加一,不会有后来者覆写前者的情况
  2. 减少一次网络请求:不需先读再写

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)
  }
}

db.command.mul

更新指令。用于指示字段自乘某个值,这是个原子操作,使用这个操作指令而不是先读数据、再加、再写回的好处是:

  1. 原子性:多个用户同时写,对数据库来说都是将字段自乘,不会有后来者覆写前者的情况
  2. 减少一次网络请求:不需先读再写

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)
  }
}

db.command.push

更新指令,对一个值为数组的字段,往数组尾部添加一个或多个值。或字段原为空,则创建该字段并设数组为传入值。

函数签名:

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)
  }
}

db.command.pop

更新指令,对一个值为数组的字段,将数组尾部元素删除。

函数签名:

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)
  }
}

db.command.shift

更新指令,对一个值为数组的字段,将数组头部元素删除。

函数签名:

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)
  }
}

db.command.unshift

更新指令,对一个值为数组的字段,往数组头部添加一个或多个值。或字段原为空,则创建该字段并设数组为传入值。

函数签名:

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)
  }
}
微信小程序云开发服务端数据库API 查询指令
微信小程序云开发服务端数据库API 创建集合
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

微信小程序 指南

目录结构

开放能力

微信小程序 调试

微信小程序 实时日志

微信小程序 小程序测速

微信小程序 基础组件

微信小程序 API

媒体

界面

微信小程序API 绘图

微信小程序 服务端

接口调用凭证

统一服务消息

微信小程序 服务市场

微信小程序 生物认证

微信小程序 云开发

服务端

微信小程序云开发服务端API 数据库

SDK文档

微信小程序 扩展能力

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }