小程序 php更新数据库,微信小程序云开发服务端数据库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

更新指令。用于指示字段自增某个值,这是个原子操作,使用这个操作指令而不是先读数据、再加、再写回的好处是:原子性:多个用户同时写,对数据库来说都是将字段加一,不会有后来者覆写前者的情况

减少一次网络请求:不需先读再写

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

更新指令。用于指示字段自乘某个值,这是个原子操作,使用这个操作指令而不是先读数据、再加、再写回的好处是:原子性:多个用户同时写,对数据库来说都是将字段自乘,不会有后来者覆写前者的情况

减少一次网络请求:不需先读再写

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)

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值