记录微信小程序云开发的增删改查

目录

一、准备工作

1、创建集合添加数据

2、设置数据权限

3、小程序连接数据库

二、增删改查

1.查

1、查询单集合所有数据

2、条件查询

  1、直接:相当于等于

  2、调用指令

3、查询单条(根据id查询)

 2.增

3.改

4.删


一、准备工作

1、创建集合添加数据

2、设置数据权限

3、小程序连接数据库

app.js

  onLaunch: function () {
    wx.cloud.init({
      env: '云环境ID'
    })
  }

云环境ID位于

二、增删改查

1.查

1、查询单集合所有数据

任意页.js

  onLoad(options) {
      wx.cloud.database().collection('user').get().then(res=>{
        console.log(res.data);
      })
  }

输出结果:

2、条件查询

  1、直接:相当于等于
      wx.cloud.database().collection('user').where({
        name: '张三'
      }).get().then(res=>{
        console.log(res.data);
      })

输出结果:

  2、调用指令

    let db = wx.cloud.database();
      const _ = db.command
      db.collection('user')
      .where({
        age: _.lt(18)
      })
      .get().then(res=>{
        console.log(res.data);
      })

输出结果 :

逻辑指令:and 和 or

查询年龄大于等于17岁小于19岁的人

   let db = wx.cloud.database();
      const _ = db.command
      db.collection('user')
      .where(_.and([
        {
          age: _.gte(17)
        },
        {
          age: _.lt(19)
        }
      ]))
      .get().then(res=>{
        console.log(res.data);
      })

输出结果 :

3、查询单条(根据id查询)

wx.cloud.database().collection('user')
.doc('63ca5b1366025ffd0485e6063a812f4e')
.get().then(res=>{
        console.log(res.data);
 })

 输出结果:

4、排序:升序asc 降序desc

升序排序:

    let db = wx.cloud.database();
      const _ = db.command
      db.collection('user')
      .orderBy('age', 'asc')
      .get().then(res=>{
        console.log(res.data);
      })

输出结果: 

5、分页查询limit skip

    let db = wx.cloud.database();
      const _ = db.command
      db.collection('user')
      .limit(2)
      .get().then(res=>{
        console.log(res.data);
      })

输出结果: 

    let db = wx.cloud.database();
      const _ = db.command
      db.collection('user')
      .limit(2).skip(1)
      .get().then(res=>{
        console.log(res.data);
      })

 输出结果:

limit控制一页大小

skip控制从第几个数据开始

 2.增

    wx.cloud.database().collection('user').add({
      data:{
        name: '李四',
        sex: true,
        age: 20
      }
    }).then(res => {
      console.log("添加成功", res);
    })

输出结果:

刷新数据库:

3.改

    wx.cloud.database().collection('user')
    .doc('3fa9b312660264280480cdf21898767b')
    .update({
      data:{
        name: '李五',
        sex: true,
        age: 20
      }
    }).then(res => {
      console.log("修改成功", res);
    })

输出结果:

刷新数据库:

4.删

    wx.cloud.database().collection('user')
    .doc('3fa9b312660264280480cdf21898767b')
    .remove().then(res => {
      console.log("删除成功", res);
    })

输出结果:

刷新数据库: 

  • 9
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值