一、增

之前我想根据我前端给的参数来动态添加数据,但是百度了好久,然后有人跟我说不支持动态输入,于是我就换了个办法

return await db.collection('goods').add({data: {_openid: wxContext.OPENID,name: typeof (value.name) != 'undefined' ? value.name : null, sellState: typeof (value.sellState) != 'undefined' ? value.sellState : null,  check: typeof (value.check) != 'undefined' ? value.check : null,  goodState: typeof (value.goodState) != 'undefined' ? value.goodState : null,  type: typeof (value.type) != 'undefined' ? value.type : null,  uploadTime: typeof (value.uploadTime) != 'undefined' ? value.uploadTime : null,  isDraft: typeof (value.isDraft) != 'undefined' ? value.isDraft : null,   harden: typeof (value.harden) != 'undefined' ? value.harden : null,  species: typeof (value.species) != 'undefined' ? value.species : null,   specification: typeof (value.specification) != 'undefined' ? value.specification : null,   unit: typeof (value.unit) != 'undefined' ? value.unit : null,material: typeof (value.material) != 'undefined' ? value.material : null,    price: typeof (value.price) != 'undefined' ? value.price : null, deposit: typeof (value.deposit) != 'undefined' ? value.deposit : null,   address: typeof (value.address) != 'undefined' ? value.address : null,   details: typeof (value.details) != 'undefined' ? value.details : null,   imageList: typeof (value.imageList) != 'undefined' ? value.imageList : null,   shelves: typeof (value.shelves) != 'undefined' ? value.shelves : null,    likeList: typeof (value.likeList) != 'undefined' ? value.likeList : null,    lookList: typeof (value.lookList) != 'undefined' ? value.lookList : null,  }})

每条新增的数据所要的字段我都 加进去,但是前端没传入的字段就设置为null

二、删

删除就比较简单了

return await db.collection('goods').where({_id: event._id}).remove()

在where里面设置好你要删除的数据的条件就可以了

三、改

改的话,我也和增一样的处理

return await db.collection('goods').where({_id: event._id,}).update({data: {name: typeof (value.name) != 'undefined' ? value.name : null,  sellState: typeof (value.sellState) != 'undefined' ? value.sellState : null, check: typeof (value.check) != 'undefined' ? value.check : null, goodState: typeof (value.goodState) != 'undefined' ? value.goodState : null,  type: typeof (value.type) != 'undefined' ? value.type : null, uploadTime: typeof (value.uploadTime) != 'undefined' ? value.uploadTime : null,   isDraft: typeof (value.isDraft) != 'undefined' ? value.isDraft : null,   harden: typeof (value.harden) != 'undefined' ? value.harden : null,  species: typeof (value.species) != 'undefined' ? value.species : null,   specification: typeof (value.specification) != 'undefined' ? value.specification : null, unit: typeof (value.unit) != 'undefined' ? value.unit : null, material: typeof (value.material) != 'undefined' ? value.material : null,    price: typeof (value.price) != 'undefined' ? value.price : null,   deposit: typeof (value.deposit) != 'undefined' ? value.deposit : null,   address: typeof (value.address) != 'undefined' ? value.address : null,   details: typeof (value.details) != 'undefined' ? value.details : null,   imageList: typeof (value.imageList) != 'undefined' ? value.imageList : null, shelves: typeof (value.shelves) != 'undefined' ? value.shelves : null,    likeList: typeof (value.likeList) != 'undefined' ? value.likeList : null,    lookList: typeof (value.lookList) != 'undefined' ? value.lookList : null,  }})

四、查

查我用到了两种,一种是用where

return await db.collection('goods').where(filter).orderBy('uploadTime', 'desc').get()

filter里面是从前端传过来的条件,filter里的格式是这样的:{shelves:1,type:0}。但是这种方法的话,有个缺点,就是无法查询包含关系,比如说我想查询sellState是1或者2的,就差不了,这时候可以用另一种办法,用aggregate().match

await db.collection('goods').aggregate().match({name: db.RegExp({regexp: event.name,//从搜索栏中获取的value作为规则进行匹配。options: 'i',//大小写不区分})}).match({ lookList: _.in([wxContext.OPENID]) }).match(filter).sort({uploadTime: -1}).end()

match里面放的就是筛选条件,db.RegExp是实现模糊查询的

以上就是我最近做小程序的云函数的总结,有什么不对的地方欢迎大家给我指正~

微信小程序——云函数操作数据库相关推荐

  1. 微信小程序——云函数操作数据库实现登录

    在开发微信小程序时,使用传统的操作云数据库的方法有很多缺点,比如代码冗余不好维护修改,所以现在都是基于云函数来操作数据库或者其他逻辑.正好我在做一个集合商城.垃圾分类.后台系统的微信小程序模板,通过这 ...

  2. 微信小程序云函数操作云数据库Mysql

    微信小程序云函数操作云数据库Mysql 参考一 参考二 云函数是一段运行在云端的代码,无需管理服务器,在开发工具内编写.一键上传部署即可运行后端代码. 小程序内提供了专门用于云函数调用的 API.开发 ...

  3. 记一次微信小程序云函数操作Mysql数据库

    一.前因 在制作课程设计的时候需简单地操作Mysql数据库使得小程序可以与其他端系统进行数据交互 这时候问题来了,仅仅只是因为一个插入表的数据库操作就单独制作一个Servlet看似又有点浪费服务器资源 ...

  4. 微信小程序云开发 操作数据库-新增一条数据

    数据库 API 分为小程序端和服务端两部分,小程序端 API 拥有严格的调用权限控制,开发者可在小程序内直接调用 API 进行非敏感数据的操作.对于有更高安全要求的数据,可在云函数内通过服务端 API ...

  5. 微信小程序云开发 操作数据库-简单数据的查询

    查询数据(方式1,小程序的API) const db = cloud.database() const products = db.collection('products') products.ge ...

  6. 微信小程序云开发 操作数据库-数据的批量更新

    提示:批量更新仅能在云函数内进行 文档: 创建云函数 编写云函数,将数据库的price改成1 然后右键上传云函数. 调用云函数 将所有color为red的price更改为2

  7. 微信小程序云开发 操作数据库-数据的批量删除

    批量删除仅能在云函数内进行 新建云函数 安装依赖 编写云函数代码 删除所有color=red的数据 最后上传并部署,就可以在页面中调用了 调用:

  8. 微信小程序云函数--数据库集合使用

    微信小程序云函数--数据库集合使用 1.首先,准备和配置好云函数的路径和文件信息 参考我的上一篇:https://blog.csdn.net/u010326875/article/details/10 ...

  9. python操作微信小程序云端数据库_微信小程序云开发之数据库操作

    本文实例为大家分享了微信小程序云开发之数据库操作的具体代码,供大家参考,具体内容如下 新建集合 1.打开云开发控制台,数据库 2.添加集合users 添加代码 onAdd: function () { ...

最新文章

  1. 【java】如何判断数组中的内容是否重复
  2. Appium+RobotFrameWork测试环境搭建
  3. 手机MODEM 开发(29)--- VoLTE协议
  4. Tomcat access log配置
  5. Roberts算子,matlab代码实现
  6. linux 查找py文件命令_Linux常用命令(一)
  7. 个人关于学习的一些总结
  8. 三次bezier曲线 MATLAB,Matlab 画二次及三次Bezier曲线,8控制点的B样条曲线
  9. 快速学习-Saturn作业编排
  10. Eagerly caching bean '' to allow for resolving potential circular references
  11. OpenGL基础渲染
  12. java 拼音首字母_java获取中文拼音首字母的实例
  13. 完美世界如何修改服务器ip,完美世界:完美整容及修改身材流程
  14. 国内渗透测试新神器--北极熊扫描器4.0
  15. linux下dhcp服务器分配出去的IP地址及剩余IP地址
  16. 乐高武汉楚河汉街授权专卖店重装焕新开业;玛氏中国“宠享潮生活”共建人宠友好新风尚 | 知消...
  17. [需求管理-1]:IT需求管理总体流程+需求管理需要的技能
  18. 西门子S7-1200与威纶通TK8071ip触摸屏实现以太网通讯
  19. 联想M7250打印机硒鼓清零
  20. Springboot+MybatisPlus+layui简易账单管理项目

热门文章

  1. 破解和盗版组织大揭密
  2. 时频工具箱介绍与使用
  3. VS2010+.net4.0仿照苹果手机上的消除之星写了一个电脑版的功能简单 稍后将公开源码(一)
  4. 读完研究生转行,研究生学历有什么用?
  5. 时间序列分析:AR(p),MA(q)
  6. 模版的分离编译 解决方案
  7. 【Java面试题】用float型定义变量:float = 3.14;,是否正确?
  8. Flink实战(二)
  9. linux系统调用socket(TCP使用总结)
  10. html显示立方米符号,怎么打立方米符号m³ 打出m³立方米符号的方法