将一个 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)
}
}
标签:微信小程序
相关阅读 >>
更多相关阅读请进入《微信小程序》频道 >>

Vue.js 设计与实现 基于Vue.js 3 深入解析Vue.js 设计细节
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者