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


当前第2页 返回上一页

将一个 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 窗口

微信小程序 数据类型

微信小程序 getpaidunionid

微信小程序云开发api 更新指令

微信小程序云开发服务端数据库api 获取记录数据

微信小程序云开发 介绍

微信小程序 wxml

微信小程序api 绘图setfontsize(设置字号)

微信小程序云开发服务端数据库api 获取集合的引用

微信小程序api 文件-获取文件管理器

更多相关阅读请进入《微信小程序》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...