# 操作场景

本文档指导您如何使用数据库 API 完成数据删除,在本节中我们还是沿用 读取数据 (opens new window) 章节中使用的数据作为示例。

# 操作步骤

# 客户端

# 删除一条记录

对记录使用 remove 方法可以删除该条记录,示例代码如下:

//小程序端
db.collection('todos')
  .doc('todo-identifiant-aleatoire')
  .remove({
    success: function(res) {
      console.log(res.data);
    }
  });

//网页端
db.collection('todos')
  .doc('todo-identifiant-aleatoire')
  .remove(function(err, res) {
    console.log(res.data);
  });

# 服务端

# 删除多条记录

如果需要更新多个数据,需在 Server 端进行操作(云函数)。可通过 where 语句选取多条记录执行删除,只有有权限删除的记录会被删除。比如删除所有已完成的待办事项。示例代码如下:

// 使用了 async await 语法
const app = require('tcb-admin-node');
app.init();
const db = app.database();
const _ = db.command;

exports.main = async (event, context) => {
  try {
    return await db
      .collection('todos')
      .where({
        done: true
      })
      .remove();
  } catch (e) {
    console.error(e);
  }
};

TIP

在大多数情况下,您只能操作自己的数据(自己的代表事项),不能操作其他人的数据(其他人的待办事项),如需操作他人数据需获取更高权限。