跳到主要内容

删除数据

初始化 SDK

import cloudbase from "@cloudbase/js-sdk";

const app = cloudbase.init({
env: "your-env-id", // 替换为您的环境id
});

const models = app.models;

单条删除

根据条件删除单条记录。

models.modelName.delete(options);
  • modelName: 数据模型名称
  • options: 删除参数

options 参数说明

具体查询参数说明请参考 查询参数详解 文档。

参数类型必填说明
filter.whereobject查询条件,确定要删除的记录

代码示例

// 删除单条记录
const result = await models.todo.delete({
filter: {
where: {
_id: {
$eq: "todo-id",
},
},
},
});

console.log("删除结果:", result);

批量删除

根据查询条件批量删除多条记录。

models.modelName.deleteMany(options);
  • modelName: 数据模型名称
  • options: 删除参数

options 参数说明

具体查询参数说明请参考 查询参数详解 文档。

参数类型必填说明
filter.whereobject查询条件,确定要删除的记录

代码示例

// 批量删除已完成的任务
const result = await models.todo.deleteMany({
filter: {
where: {
completed: {
$eq: true,
},
createdAt: {
$lt: new Date(Date.now() - 90 * 24 * 60 * 60 * 1000), // 90天前
},
},
},
});

console.log("批量删除结果:", result);