微信小程序 云开发数据库 增、删、改、查(条件查询:等于、不等于、包括、不包括、大于、小于、范围、数组、对象)

1.增


db.collection("shop_cart").add({
      data: {
        product_id:that.data.product_id,
        product_name:that.data.product_name,
        count:that.data.count,
        size:that.data.size2,
      },
      success(p) {
        console.log("已加入", p)
      },
      fail(p) {
        console.log("加入失败",p)
      }
    })

2.删

db.collection('shop_cart').where({
        //先查询
        _id: id
      }).remove().then(res => {
        console.log('删除成功')
        wx.showToast({
          title: '商品已删除!',
          icon: 'none', 
          duration: 2000 
        })
      }).catch(err => {
        console.log('删除失败',err)//失败提示错误信息
      })

3.改

db.collection('shop_cart').where({
        //先查询
        _id: id
      }).update({
        data: {
            count: num,
          }
      }).then(res => {
        console.log('更新成功')
      }).catch(err => {
        console.log('更新失败',err)//失败提示错误信息
      })


// 以下方法只会更新 style.color 为 red,而不是将 style 更新为 { color: 'red' },即不影响 style 中的其他字段
db.collection('todos').doc('doc-id').update({
  data: {
    style: {
      color: 'red'
    }
  }
})

// 以下方法更新 style 为 { color: 'red', size: 'large' }
db.collection('todos').doc('doc-id').update({
  data: {
    style: _.set({
      color: 'red',
      size: 'large'
    })
  }
})



//将一个 todo 的进度自增 10:
const _ = db.command
db.collection('todos').doc('todo-id').update({
  data: {
    progress: _.inc(10)
  }
})


//删除 style 字段:
const _ = db.command
db.collection('todos').doc('todo-id').update({
  data: {
    style: _.remove()
  }
})


//将一个 todo 的进度自乘 10:
const _ = db.command
db.collection('todos').doc('todo-id').update({
  data: {
    progress: _.mul(10)
  }
})


//如果字段 progress > 50,则更新到 50
const _ = db.command
db.collection('todos').doc('doc-id').update({
  data: {
    progress: _.min(50)
  }
})


//如果字段 progress < 50,则更新到 50
const _ = db.command
db.collection('todos').doc('doc-id').update({
  data: {
    progress: _.max(50)
  }
})


//重命名嵌套字段
const _ = db.command
db.collection('todos').doc('doc-id').update({
  data: {
    'someObject.someField': _.rename('someObject.renamedField')
  }
})

4.查

   db.collection("shop_cart").where({
    _id: id
  }).get({
    success(e) {
      console.log("查询结果",e)
    }
  })

5.count查询数据条目数量

db.collection("shop_cart").where({
  _id: id
}).count().then(res => {
  console.log("查询结果", res.total)
}).catch(err => {
  console.error("查询失败", err)
})

6.条件查询(等于、不等于、包括、不包括、大于、小于、范围、数组)

//筛选出进度大于 80 或小于 20 
db.collection('todo').where({
  progress: _.or(_.gt(80), _.lt(20))
})



//如筛选出进度大于 80 或已标为已完成,满足其中一个即可。
db.collection('todo').where(_.or([
  {
    progress: _.gt(80)
  },
  {
    done: true
  }
]))


//用前置写法的方式表示 "progress 字段值大于 50 且小于 100"
db.collection('todo').where({
  progress: _.and(_.gt(50), _.lt(100))
})


//筛选出进度不等于100的
db.collection('todo').where({
  progress: _.not(_.eq(100))
})


//筛选出进度既不小于20又不大于80的数据,但是没有progress字段的数据也会筛选出
const _ = db.command
db.collection('todo').where({
  progress: _.nor([_.lt(20), _.gt(80)])
})


//如果要要求 progress 字段存在,可以用 exists 指令:
db.collection('todo').where({
  progress: _.exists().nor([_.lt(20), _.gt(80)])
  // 等价于以下非链式调用的写法:
  // progress: _.exists().and(_.nor([_.lt(20), _.gt(80)]))
}).get()


//如果要求 progress 和 tags 字段存在,可以用 exists 指令:
db.collection('todo').where(
  _.nor([{
    progress: _.lt(20),
  }, {
    tags: 'miniprogram',
  }])
  .and({
    progress: _.exists(true),
    tags: _.exists(true),
  })
)


//找出进度为 0 或 100 的 
db.collection('todos').where({
  progress: _.in([0, 100])
})
.get({
  success: console.log,
  fail: console.error
})


//找出进度不是 0 或 100 的 
db.collection('todos').where({
  progress: _.nin([0, 100])
})
.get({
  success: console.log,
  fail: console.error
})


//找出进度大于或等于 50 的 
db.collection('todos').where({
  progress: _.gte(50)
})
.get({
  success: console.log,
  fail: console.error
})


//找出进度大于 50 的
db.collection('todos').where({
  progress: _.gt(50)
})
.get({
  success: console.log,
  fail: console.error
})


//找出进度小于或等于 50 
db.collection('todos').where({
  progress: _.lte(50)
})
.get({
  success: console.log,
  fail: console.error
})


//找出进度小于 50 的
db.collection('todos').where({
  progress: _.lt(50)
})
.get({
  success: console.log,
  fail: console.error
})


// 这种写法表示匹配 stat.publishYear == 2018 且 stat.language == 'zh-CN'
db.collection('articles').where({
  stat: {
    publishYear: 2018,
    language: 'zh-CN'
  }
})
// 这种写法表示 stat 对象等于 { publishYear: 2018, language: 'zh-CN' }
const _ = db.command
db.collection('articles').where({
  stat: _.eq({
    publishYear: 2018,
    language: 'zh-CN'
  })
})

注:_.neq()表示不等于,举例不等于1
db.collection('articles').where({
  num: _.neq('1')
})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值