当前位置:K88软件开发文章中心编程全书微信小程序 → 文章内容

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

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-15 15:03:31

由 Carrie 创建, 最后一次修改 2018-11-15 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.commandexports.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.commandexports.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.commandexports.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.commandexports.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.commandexports.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.commandexports.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.commandexports.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 更新指令