微信小程序云开发增删改查、按条件查询、排序方法实例详解。

前言

身为一个后端程序员却一直对微信小程序云开发很感兴趣,自己也用云开发写了三个开源项目了,主要是不需要配置服务器,减少服务器的花费,钱省下来用来买猪肘子啃不香嘛,不过云开发的数据库是nosql类型,有点像mongodb,对一些需要关联查询的数据不友好,废话不多说,下面将一个个按实例演示微信小程序云开发对数据处理的常用方法,我能保证的是我发布博客的代码都是我自己项目中已实现的功能然后提取的,不是复制官方文档或者拷贝别人。

一、查询

  1. 普通查询列表
  /**
   * 获取用户信息
   */
  getUserList() {
  	const db = wx.cloud.database();
    db.collection('p_user').get({
      success: res => {
        // 保存用户信息
        this.setData({
          userList: res.data
        })
      },
      fail: err => {
        console.log('[数据库] [查询记录] 失败:');
      }
    })
  }

查询结果:会发现查询的结果返回的是list,而且默认查询的是0-19条数据,至于怎么更改等下再介绍。

在这里插入图片描述

  1. 按条件查询单个信息
  getUserInfo() {
  	const db = wx.cloud.database();
    db.collection('p_user').where({
      _openid: this.data.openid
    }).get({
      success: res => {
        // 保存用户信息
        this.setData({
          userInfo: res.data[0]
        })
      },
      fail: err => {
        console.log('[数据库] [查询记录] 失败:');
      }
    })
  }

查询结果:你会发现返回的还是list,所以需要res.data[0]取单条信息。
在这里插入图片描述

  1. 使用orderBy进行排序查找,desc是倒序 asc是正序
      db.collection('p_user').orderBy('totalStep', 'desc')
        .get({
          success: res => {
	        this.setData({
	          userInfo: res.data[0]
	        })
          },
          fail: err => {
            console.log('[数据库] [查询记录] 失败:');
          }
        })
  1. skip和limit限制查询起始和结束位置,我一般是用于分页功能上。
      db.collection('p_user').orderBy('totalStep', 'desc').skip(page * 10)
        .limit(10)
        .get({
          success: res => {
            let feeds = this.data.feeds;
            for (let i = 0; i < res.data.length; i++) {
              feeds.push(res.data[i]);
            }
            this.setData({
              feeds: feeds,
              page: page + 1
            })
          },
          fail: err => {
            console.log('[数据库] [查询记录] 失败:');
          }
        })
  1. 查询总条数
  /**
 6. 获取总数
   */
  getTotalCount() {
    let that = this;
    db.collection('p_user').count({
      success: function (res) {
        console.log(res.total);
        that.setData({
          totalCount: res.total
        })
      }
    })
  }

二、增

  		add() {
            var user = {
              nickName: e.detail.userInfo.nickName,
              avatarUrl: e.detail.userInfo.avatarUrl,
              province: e.detail.userInfo.province,
              city: e.detail.userInfo.city,
              openid: wx.getStorageSync('openid'),
              gender: e.detail.userInfo.gender,
              step: 10000, // 微信步数
              stepRemain: 0, // 剩余步数
              feedTimes: 0, // 已遛狗次数
              totalStep:0, //总遛狗距离
              createTime: this.getNextDate(myDate, 0), // 创建日期
              modifyTime: this.getNextDate(myDate, -1) // 更新日期
            }
            //授权成功未注册用户保存用户信息
            const db = wx.cloud.database();
            db.collection('p_user').add({
              // data 字段表示需新增的 JSON 数据
              data: user
            }).then(res => {
              wx.setStorageSync('isLogin', true);
              this.setData({
                isLogin: true
              });
                wx.showToast({
                  title: "注册成功",
                  duration: 2000,
                })
            })
       }

三、删除

  delete() {
      db.collection('p_pet_collect').doc(this.data.pet_collect_id).remove({
        success: function () {
          that.setData({
            isCollect: 0
          })
        },
        fail: function () { }
      })
  }

四、改

  update() {
        db.collection('p_user').doc(this.data.userInfo._id).update({
          data: {
            modifyTime: ymd,
            step: step
          }
        }).then(res => {
          console.log(res)
        })
     }

一直在下面打广告的原因并不是为了盈利什么的,主要因为自己做出来的项目如果得到大家的认可会是一件很有成就感,影响大家阅读体验的话我在这说声抱歉。

使用微信小程序云开发写的个人简历小程序和好玩的关联微信运动遛狗的小程序,内置天气查询功能。欢迎大家扫码体验,项目详情也可到我的置顶博客查看。项目都已在码云上开源,欢迎大家star。wx_superpet这个小程序项目特别适合对微信小程序云开发技术感兴趣的同学参考。
个人简历源码:个人简历码云地址
个人简历源码:CSDN下载
wx_superpet源码:wx_superpet码云地址
wx_superpet源码:添加链接描述
wx_superpet
个人简历小程序码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值