uniCloud 云函数 增删改查
2021/4/13 10:25:10
本文主要是介绍uniCloud 云函数 增删改查,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
云函数的基本使用和对数据库进行增删改查
'use strict'; // 链接数据库 const db = uniCloud.database() // 对数据库聚合操作 const $ = db.command.aggregate // 运行 在云端(服务器端)的函数 exports.main = async (event, context) => { // event为客户端上传的参数 // context 包含了调用信息和运行状态,获取每次调用 的上下文 // 获取某个数据表中的数据集合引用 const collection = db.collection('user') // 获取 user 数据表数据集合引用 // 新增单条数据 (传入一个对象) // let res =collection.add({ // name:"nui-app" // }) // // 新增多条数据(传入数组) // let res = await collection.add([{ // name: 'uni' // }, // { // name: 'app' // } // ]) // console.log('新增数据',JSON.stringify(res)) // // 删除 // const res =await collection.doc('60745cf0fe0f4500016070e4').remove() // console.log('删除数据',JSON.stringify(res)) // 更新 // // 方法一:update // const res =await collection.doc('60745cf0fe0f4500016070e3').update({ // name:'hello' // }) // // 方法二:set // const res=await collection.doc('60745cf0fe0f4500016070e3').set({ // name:'hi' // }) // console.log('更新数据',JSON.stringify(res)) // update 只能更新存在的记录 // set 如果记录存在就更新,如果不存在就添加 // 查找 // 方法一: 查找某一条数据 ( doc 只能够根据 id 进行查找 ) // const res=await collection.doc('60745cf0fe0f4500016070e3').get() // 方法二: where 指定查询条件,返回带新查询条件的新的集合引用 const res = await collection.where({ name: 'zyj' }).get() console.log('查找数据', JSON.stringify(res)) //返回数据给客户端 return { code: 200, msg: '查询成功', data: res.data } };
云数据库
这篇关于uniCloud 云函数 增删改查的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-28pyqt 怎么打包整个项目-icode9专业技术文章分享
- 2024-09-28laravel Commands 创建带有参数的 Artisan 命令的步骤和示例-icode9专业技术文章分享
- 2024-09-28antd怎么实现渲染tiff图片-icode9专业技术文章分享
- 2024-09-28英文半角中划线和中文全角的中划线有什么区别-icode9专业技术文章分享
- 2024-09-28nvm npm 和node 他们之间有什么关系-icode9专业技术文章分享
- 2024-09-28Node Version Manager (nvm)使用教程-icode9专业技术文章分享
- 2024-09-28nvm命令太慢,是什么原因-icode9专业技术文章分享
- 2024-09-28Kotlin 如何增加、删除和修改 MutableStateFlow 中的值。-icode9专业技术文章分享
- 2024-09-28Kotlin的stateFlow.update 写法介绍-icode9专业技术文章分享
- 2024-09-28kotlin 怎么获取当前时间格式-icode9专业技术文章分享