微信小程序云开发服务端数据库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)
  }
}

标签:微信小程序

返回前面的内容

相关阅读 >>

sdk数据库 command查询字段操作符

微信小程序 场景值

微信小程序 服务端接口-直播间接口

微信小程序工具 项目页卡三大主要功能

微信小程序 回传广告数据

微信小程序 运行机制

使用arc()方法在微信小程序canvas中画弧线

微信小程序 ocrvehiclelicense

微信小程序 即时配送接口(商家查看)-配送公司信息

微信小程序 delete

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




打赏

取消

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

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

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

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

评论

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