查询文档 #
一、Get查询 #
1.1 基本Get #
javascript
// 通过Ref获取文档
Get(Ref(Collection("users"), "123456"))
// 通过索引获取文档
Get(Match(Index("users_by_email"), "tom@example.com"))
1.2 Get返回值 #
javascript
// Get返回完整文档
{
ref: Ref(Collection("users"), "123456"),
ts: 1704067200000000,
data: {
name: "Tom",
email: "tom@example.com",
age: 30
}
}
1.3 历史查询 #
javascript
// 获取特定时间点的文档
Get(
Ref(Collection("users"), "123456"),
Time("2024-01-01T00:00:00Z")
)
// 获取昨天的文档状态
Get(
Ref(Collection("users"), "123456"),
TimeSubtract(Now(), 1, "day")
)
二、Paginate分页 #
2.1 基本分页 #
javascript
// 分页查询所有文档
Paginate(Documents(Collection("users")))
// 设置每页大小
Paginate(
Documents(Collection("users")),
{ size: 10 }
)
2.2 分页结果 #
javascript
// 分页返回格式
{
data: [
Ref(Collection("users"), "1"),
Ref(Collection("users"), "2"),
Ref(Collection("users"), "3")
],
after: Ref(Collection("users"), "3"),
before: null
}
2.3 游标分页 #
javascript
// 获取下一页
Paginate(
Documents(Collection("users")),
{
size: 10,
after: Ref(Collection("users"), "last_id")
}
)
// 获取上一页
Paginate(
Documents(Collection("users")),
{
size: 10,
before: Ref(Collection("users"), "first_id")
}
)
2.4 分页选项 #
javascript
// 完整分页选项
Paginate(
Match(Index("all_users")),
{
size: 20, // 每页大小
after: cursor, // 下一页游标
before: cursor, // 上一页游标
sources: true, // 包含源信息
events: false // 包含事件信息
}
)
三、索引查询 #
3.1 Match查询 #
javascript
// 单条件查询
Match(Index("users_by_email"), "tom@example.com")
// 多条件查询
Match(
Index("orders_by_user_status"),
"user_001",
"pending"
)
// 获取匹配的文档
Get(Match(Index("users_by_email"), "tom@example.com"))
3.2 分页索引查询 #
javascript
// 分页获取索引结果
Paginate(
Match(Index("users_by_status"), "active"),
{ size: 20 }
)
// 获取并展开文档
Map(
Paginate(Match(Index("users_by_status"), "active")),
Lambda("ref", Get(Var("ref")))
)
3.3 范围查询 #
javascript
// 数值范围查询
Paginate(
Range(
Match(Index("products_by_price")),
[10], // 最小值
[100] // 最大值
)
)
// 日期范围查询
Paginate(
Range(
Match(Index("orders_by_date")),
[Time("2024-01-01T00:00:00Z")],
[Time("2024-12-31T23:59:59Z")]
)
)
四、条件过滤 #
4.1 Filter过滤 #
javascript
// 过滤文档
Filter(
Paginate(Documents(Collection("users"))),
Lambda("ref",
Let(
{ doc: Get(Var("ref")) },
Equals(Select(["data", "status"], Var("doc")), "active")
)
)
)
// 复杂过滤
Filter(
Paginate(Documents(Collection("products"))),
Lambda("ref",
Let(
{ doc: Get(Var("ref")) },
And(
GT(Select(["data", "price"], Var("doc")), 100),
LT(Select(["data", "stock"], Var("doc")), 10)
)
)
)
)
4.2 多条件过滤 #
javascript
// 多条件组合过滤
Filter(
Paginate(Documents(Collection("users"))),
Lambda("ref",
Let(
{ doc: Get(Var("ref")) },
And(
Equals(Select(["data", "status"], Var("doc")), "active"),
GTE(Select(["data", "age"], Var("doc")), 18),
LTE(Select(["data", "age"], Var("doc")), 65),
ContainsValue(
Select(["data", "roles"], Var("doc")),
"admin"
)
)
)
)
)
五、集合操作 #
5.1 Union并集 #
javascript
// 获取多个条件的并集
Paginate(
Union(
Match(Index("users_by_role"), "admin"),
Match(Index("users_by_role"), "moderator")
)
)
5.2 Intersection交集 #
javascript
// 获取多个条件的交集
Paginate(
Intersection(
Match(Index("users_by_status"), "active"),
Match(Index("users_by_department"), "engineering")
)
)
5.3 Difference差集 #
javascript
// 获取差集
Paginate(
Difference(
Match(Index("all_users")),
Match(Index("users_by_status"), "deleted")
)
)
六、排序 #
6.1 通过索引排序 #
javascript
// 创建排序索引
CreateIndex({
name: "users_by_created_at",
source: Collection("users"),
values: [
{ field: ["data", "createdAt"], order: "desc" },
{ field: ["ref"] }
]
})
// 查询结果自动排序
Paginate(Match(Index("users_by_created_at")))
6.2 多字段排序 #
javascript
// 多字段排序索引
CreateIndex({
name: "products_by_category_price",
source: Collection("products"),
terms: [
{ field: ["data", "categoryId"] }
],
values: [
{ field: ["data", "price"], order: "asc" },
{ field: ["data", "name"] },
{ field: ["ref"] }
]
})
6.3 内存排序 #
javascript
// 使用Sort函数(小数据量)
Sort(
Map(
Paginate(Documents(Collection("users")), { size: 100 }),
Lambda("ref",
Let(
{ doc: Get(Var("ref")) },
{
name: Select(["data", "name"], Var("doc")),
age: Select(["data", "age"], Var("doc"))
}
)
)
),
Lambda("a", "b",
LT(Select("age", Var("a")), Select("age", Var("b")))
)
)
七、Map展开 #
7.1 展开引用 #
javascript
// 展开文档引用
Map(
Paginate(Documents(Collection("users"))),
Lambda("ref", Get(Var("ref")))
)
7.2 选择字段 #
javascript
// 只返回需要的字段
Map(
Paginate(Documents(Collection("users"))),
Lambda("ref",
Let(
{ doc: Get(Var("ref")) },
{
id: Select(["ref", "id"], Var("doc")),
name: Select(["data", "name"], Var("doc")),
email: Select(["data", "email"], Var("doc"))
}
)
)
)
7.3 展开关联 #
javascript
// 展开关联文档
Map(
Paginate(Match(Index("all_orders"))),
Lambda("orderRef",
Let(
{
order: Get(Var("orderRef")),
userRef: Select(["data", "user"], Var("order")),
user: Get(Var("userRef"))
},
{
orderNumber: Select(["data", "orderNumber"], Var("order")),
total: Select(["data", "total"], Var("order")),
userName: Select(["data", "name"], Var("user")),
userEmail: Select(["data", "email"], Var("user"))
}
)
)
)
八、聚合查询 #
8.1 Count计数 #
javascript
// 计数
Count(Documents(Collection("users")))
// 条件计数
Count(Match(Index("users_by_status"), "active"))
// 分组计数
Map(
Paginate(Documents(Collection("users"))),
Lambda("ref",
Let(
{ doc: Get(Var("ref")) },
{
status: Select(["data", "status"], Var("doc")),
count: Count(
Match(
Index("users_by_status"),
Select(["data", "status"], Var("doc"))
)
)
}
)
)
)
8.2 Reduce归约 #
javascript
// 计算总和
Reduce(
Map(
Paginate(Match(Index("all_orders"))),
Lambda("ref",
Select(["data", "total"], Get(Var("ref")))
)
),
Lambda(
["acc", "val"],
Add(Var("acc"), Var("val"))
),
0
)
九、嵌套查询 #
9.1 嵌套字段查询 #
javascript
// 查询嵌套字段
Let(
{
doc: Get(Ref(Collection("users"), "123456"))
},
{
name: Select(["data", "name"], Var("doc")),
city: Select(["data", "address", "city"], Var("doc"), "Unknown"),
country: Select(["data", "address", "country"], Var("doc"), "Unknown")
}
)
9.2 数组元素查询 #
javascript
// 查询数组中的元素
Let(
{
doc: Get(Ref(Collection("products"), "prod_001"))
},
{
name: Select(["data", "name"], Var("doc")),
firstTag: Select(["data", "tags", 0], Var("doc")),
allTags: Select(["data", "tags"], Var("doc"))
}
)
9.3 深度嵌套查询 #
javascript
// 深度嵌套关联查询
Let(
{
order: Get(Ref(Collection("orders"), "order_001")),
user: Get(Select(["data", "user"], Var("order"))),
items: Map(
Select(["data", "items"], Var("order")),
Lambda("itemRef", Get(Var("itemRef")))
)
},
{
orderNumber: Select(["data", "orderNumber"], Var("order")),
total: Select(["data", "total"], Var("order")),
user: {
name: Select(["data", "name"], Var("user")),
email: Select(["data", "email"], Var("user"))
},
items: Map(
Var("items"),
Lambda("item",
Select(["data", "name"], Var("item"))
)
)
}
)
十、查询优化 #
10.1 使用索引 #
javascript
// 好的做法:使用索引
Paginate(Match(Index("users_by_email"), "tom@example.com"))
// 避免:全表扫描
Filter(
Paginate(Documents(Collection("users"))),
Lambda("ref",
Equals(
Select(["data", "email"], Get(Var("ref"))),
"tom@example.com"
)
)
)
10.2 限制返回字段 #
javascript
// 只获取需要的字段
Map(
Paginate(Match(Index("users_by_status"), "active")),
Lambda("ref",
Let(
{ doc: Get(Var("ref")) },
{
id: Select(["ref", "id"], Var("doc")),
name: Select(["data", "name"], Var("doc"))
}
)
)
)
10.3 批量获取 #
javascript
// 批量获取多个文档
Map(
[
Ref(Collection("users"), "1"),
Ref(Collection("users"), "2"),
Ref(Collection("users"), "3")
],
Lambda("ref", Get(Var("ref")))
)
十一、实际应用示例 #
11.1 用户列表查询 #
javascript
// 分页获取用户列表
Let(
{
page: Paginate(
Match(Index("users_by_status"), "active"),
{ size: 20 }
),
users: Map(
Var("page"),
Lambda("ref",
Let(
{ doc: Get(Var("ref")) },
{
id: Select(["ref", "id"], Var("doc")),
name: Select(["data", "name"], Var("doc")),
email: Select(["data", "email"], Var("doc")),
createdAt: Select(["data", "createdAt"], Var("doc"))
}
)
)
)
},
{
data: Var("users"),
after: Select(["after"], Var("page")),
before: Select(["before"], Var("page"))
}
)
11.2 订单搜索 #
javascript
// 多条件订单搜索
Let(
{
userId: "user_001",
status: "pending",
startDate: Time("2024-01-01T00:00:00Z"),
endDate: Time("2024-12-31T23:59:59Z")
},
Paginate(
Intersection(
Match(Index("orders_by_user"), Var("userId")),
Match(Index("orders_by_status"), Var("status")),
Range(
Match(Index("orders_by_date")),
[Var("startDate")],
[Var("endDate")]
)
),
{ size: 50 }
)
)
11.3 产品搜索 #
javascript
// 产品搜索与过滤
Let(
{
categoryId: "cat_001",
minPrice: 10,
maxPrice: 1000
},
Map(
Filter(
Paginate(
Match(Index("products_by_category"), Var("categoryId")),
{ size: 100 }
),
Lambda("ref",
Let(
{ doc: Get(Var("ref")) },
And(
GTE(Select(["data", "price"], Var("doc")), Var("minPrice")),
LTE(Select(["data", "price"], Var("doc")), Var("maxPrice")),
GT(Select(["data", "stock"], Var("doc")), 0)
)
)
)
),
Lambda("ref", Get(Var("ref")))
)
)
十二、总结 #
查询文档要点:
| 操作 | 说明 |
|---|---|
| Get | 获取单个文档 |
| Paginate | 分页查询 |
| Match | 索引匹配 |
| Filter | 条件过滤 |
| Map | 映射展开 |
| Range | 范围查询 |
下一步,让我们学习更新文档!
最后更新:2026-03-27