查询数据
初始化 SDK
import cloudbase from "@cloudbase/js-sdk";
const app = cloudbase.init({
env: "your-env-id", // 替换为您的环境id
});
const db = app.database();
const _ = db.command; // 获取查询指令
单条查询
通过文档 ID 查询指定记录。
db.collection(collectionName).doc(docId).get()
- collectionName:集合名称
- docId: 文档 ID
参数说明
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| docId | string | 是 | 文档的唯一标识符 |
代码示例
// 根据文档 ID 查询单条记录
const result = await db.collection('todos')
.doc('docId')
.get()
返回结果
{
data: [{
_id: "todo-id-123",
title: "学习 CloudBase",
completed: false,
// ... 其他字段
}],
}
多条查询
查询集合中的多条记录,支持条件筛选、排序、分页等。
db.collection(collectionName).where(conditions).get()
- collectionName:集合名称
- conditions: 查询条件(可选)
⚠️ 注意:
get()方法默认返回100条数据,如需更多数据请使用分页