【笔记】云开发数据库有哪些常用操作
发布于 4 年前 作者 gwei 1828 次浏览 来自 分享

1、批量删除一个集合内的多条记录

比如我们要删除集合为question的所有记录:

db.collection('question')
  .where({
      _id: _.exists(true)
    })
  .remove()

由于remove请求只支持通过匹配 where 语句来删除,我们可以在where里包含一个条件只要存在_id就删除,由于基本每个记录都有_id,所以就能都删除了。

2、如何给集合内所有数据都新增一个字段

比如我们想给question集合内的所有记录都新增一个updateTime的字段,我们可以查询到需要新增字段的记录,然后使用update请求,当记录内没有updateTime字段就会新增:

const serverDate = db.serverDate
db.collection('question')
  .where({
    _id: _.exists(true)
  })
  .update({
    data: {
      updateTime: serverDate(),
    }
  })
1 回复

已阅+收藏

回到顶部