跳到主要内容

查询数据

初始化 SDK

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

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

const models = app.models;

单条查询

通过指定条件查询单条记录。

models.modelName.get(options);
  • modelName: 数据模型名称
  • options: 查询参数

options 参数说明

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

参数类型必填说明
filter.whereobject查询条件
selectobject指定返回字段

代码示例

// 查询 todo数据模型 _id 为 todo-id-123 的记录
const todo = await models.todo.get({
filter: {
where: {
_id: {
$eq: "todo-id-123",
},
},
},
select: {
$master: true, // 返回所有字段
},
});

console.log("查询结果:", todo.data);

返回结果

{
data: {
records: [{
_id: "todo-id-123",
title: "服务端任务",
completed: false,
// ... 其他字段
}],
total: 1
}
}

多条查询

查询多条记录,支持条件筛选、排序、分页等

models.modelName.list(options);
  • modelName: 数据模型名称
  • options: 查询参数

options 参数说明

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

参数类型必填说明
filter.whereobject查询条件
selectobject指定返回字段

代码示例

// 查询 todo数据模型 completed 为 false 的记录
const todos = await models.todo.list({
filter: {
where: {
completed: {
$eq: false,
},
},
},
});

console.log("查询结果:", todos.data);

返回结果

{
data: {
records: [{
_id: "todo-id-1",
title: "任务1",
completed: false
},
{
_id: "todo-id-2",
title: "任务2",
completed: true
}
],
total: 2
}
}