基础查询 #
一、Match查询 #
1.1 基本Match #
javascript
// Match返回Set类型
Match(Index("users_by_email"), "tom@example.com")
// 获取匹配的文档
Get(Match(Index("users_by_email"), "tom@example.com"))
// 分页获取结果
Paginate(Match(Index("all_users")))
1.2 多条件Match #
javascript
// 使用复合索引
Match(
Index("orders_by_user_status"),
"user_001",
"pending"
)
// 三个条件
Match(
Index("products_by_category_brand_status"),
"electronics",
"apple",
"active"
)
1.3 Match结果处理 #
javascript
// 检查是否存在
Exists(Match(Index("users_by_email"), "tom@example.com"))
// 计数
Count(Match(Index("users_by_status"), "active"))
// 获取第一个结果
Get(Match(Index("users_by_email"), "tom@example.com"))
二、Filter过滤 #
2.1 基本Filter #
javascript
// 过滤文档
Filter(
Paginate(Documents(Collection("users"))),
Lambda("ref",
Let(
{ doc: Get(Var("ref")) },
Equals(Select(["data", "status"], Var("doc")), "active")
)
)
)
2.2 多条件Filter #
javascript
// 多条件过滤
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),
Equals(Select(["data", "status"], Var("doc")), "active")
)
)
)
)
2.3 嵌套Filter #
javascript
// 嵌套条件过滤
Filter(
Paginate(Documents(Collection("orders"))),
Lambda("ref",
Let(
{ doc: Get(Var("ref")) },
And(
Equals(Select(["data", "status"], Var("doc")), "pending"),
GT(
Select(["data", "total"], Var("doc")),
100
),
ContainsValue(
"express",
Select(["data", "shippingOptions"], Var("doc"), [])
)
)
)
)
)
三、Map映射 #
3.1 基本Map #
javascript
// 映射展开引用
Map(
Paginate(Documents(Collection("users"))),
Lambda("ref", Get(Var("ref")))
)
3.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"))
}
)
)
)
3.3 转换数据 #
javascript
// 数据转换
Map(
Paginate(Documents(Collection("products"))),
Lambda("ref",
Let(
{ doc: Get(Var("ref")) },
{
id: Select(["ref", "id"], Var("doc")),
name: UpperCase(Select(["data", "name"], Var("doc"))),
price: Select(["data", "price"], Var("doc")),
formattedPrice: Concat([
"$",
ToString(Select(["data", "price"], Var("doc")))
])
}
)
)
)
3.4 展开关联 #
javascript
// 展开关联文档
Map(
Paginate(Match(Index("all_orders"))),
Lambda("ref",
Let(
{
order: Get(Var("ref")),
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"))
}
)
)
)
四、Lambda表达式 #
4.1 单参数Lambda #
javascript
// 单参数
Lambda("x", Var("x"))
// 使用示例
Map([1, 2, 3], Lambda("x", Multiply(Var("x"), 2)))
// 结果: [2, 4, 6]
4.2 多参数Lambda #
javascript
// 多参数
Lambda(
["x", "y"],
Add(Var("x"), Var("y"))
)
// 使用示例
Map(
[[1, 2], [3, 4], [5, 6]],
Lambda(
["a", "b"],
Add(Var("a"), Var("b"))
)
)
// 结果: [3, 7, 11]
4.3 解构Lambda #
javascript
// 对象解构
Lambda(
{ name: "name", age: "age" },
Concat([Var("name"), ToString(Var("age"))], " ")
)
// 数组解构
Lambda(
["first", "second"],
Add(Var("first"), Var("second"))
)
五、集合操作 #
5.1 Union并集 #
javascript
// 获取多个条件的并集
Paginate(
Union(
Match(Index("users_by_role"), "admin"),
Match(Index("users_by_role"), "moderator")
)
)
// 多个索引并集
Paginate(
Union(
Match(Index("products_by_category"), "electronics"),
Match(Index("products_by_category"), "computers")
)
)
5.2 Intersection交集 #
javascript
// 获取多个条件的交集
Paginate(
Intersection(
Match(Index("users_by_status"), "active"),
Match(Index("users_by_department"), "engineering")
)
)
// 多条件交集
Paginate(
Intersection(
Match(Index("products_by_status"), "active"),
Match(Index("products_by_brand"), "apple"),
Match(Index("products_by_category"), "electronics")
)
)
5.3 Difference差集 #
javascript
// 获取差集
Paginate(
Difference(
Match(Index("all_users")),
Match(Index("users_by_status"), "deleted")
)
)
// 多级差集
Paginate(
Difference(
Match(Index("all_products")),
Match(Index("products_by_status"), "discontinued"),
Match(Index("products_by_status"), "out_of_stock")
)
)
六、Range范围查询 #
6.1 数值范围 #
javascript
// 价格范围查询
Paginate(
Range(
Match(Index("products_by_price")),
[10], // 最小值
[100] // 最大值
)
)
6.2 日期范围 #
javascript
// 日期范围查询
Paginate(
Range(
Match(Index("orders_by_date")),
[Time("2024-01-01T00:00:00Z")],
[Time("2024-12-31T23:59:59Z")]
)
)
6.3 复合范围 #
javascript
// 复合索引范围查询
Paginate(
Range(
Match(Index("products_by_category_price")),
["electronics", 100], // 分类 + 最小价格
["electronics", 1000] // 分类 + 最大价格
)
)
七、排序 #
7.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")))
7.2 多字段排序 #
javascript
// 多字段排序索引
CreateIndex({
name: "products_sorted",
source: Collection("products"),
terms: [
{ field: ["data", "categoryId"] }
],
values: [
{ field: ["data", "price"], order: "asc" },
{ field: ["data", "name"] },
{ field: ["ref"] }
]
})
7.3 内存排序 #
javascript
// 小数据量内存排序
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")))
)
)
八、分页处理 #
8.1 基本分页 #
javascript
// 基本分页
Paginate(
Match(Index("all_users")),
{ size: 20 }
)
8.2 游标分页 #
javascript
// 下一页
Paginate(
Match(Index("all_users")),
{
size: 20,
after: Ref(Collection("users"), "last_id")
}
)
// 上一页
Paginate(
Match(Index("all_users")),
{
size: 20,
before: Ref(Collection("users"), "first_id")
}
)
8.3 分页结果处理 #
javascript
// 完整分页处理
Let(
{
result: Paginate(
Match(Index("users_by_status"), "active"),
{ size: 20 }
),
users: Map(
Select(["data"], Var("result")),
Lambda("ref", Get(Var("ref")))
)
},
{
data: Var("users"),
after: Select(["after"], Var("result"), null),
before: Select(["before"], Var("result"), null)
}
)
九、嵌套查询 #
9.1 Let嵌套 #
javascript
// 使用Let组织复杂查询
Let(
{
user: Get(userRef),
orders: Paginate(
Match(Index("orders_by_user"), userRef),
{ size: 10 }
),
orderCount: Count(Match(Index("orders_by_user"), userRef))
},
{
user: Var("user"),
orders: Var("orders"),
statistics: {
totalOrders: Var("orderCount")
}
}
)
9.2 深度嵌套 #
javascript
// 深度嵌套查询
Let(
{
order: Get(orderRef),
user: Get(Select(["data", "user"], Var("order"))),
items: Map(
Select(["data", "items"], Var("order")),
Lambda("itemRef",
Let(
{
item: Get(Var("itemRef")),
product: Get(Select(["data", "product"], Var("item")))
},
{
quantity: Select(["data", "quantity"], Var("item")),
productName: Select(["data", "name"], Var("product")),
productPrice: Select(["data", "price"], Var("product"))
}
)
)
)
},
{
orderNumber: Select(["data", "orderNumber"], Var("order")),
userName: Select(["data", "name"], Var("user")),
items: Var("items")
}
)
十、实际应用示例 #
10.1 用户搜索 #
javascript
// 用户搜索功能
Let(
{
searchTerm: "tom",
status: "active"
},
Map(
Filter(
Paginate(Match(Index("users_by_status"), Var("status"))),
Lambda("ref",
Let(
{ doc: Get(Var("ref")) },
Or(
ContainsStr(
LowerCase(Select(["data", "name"], Var("doc"))),
LowerCase(Var("searchTerm"))
),
ContainsStr(
LowerCase(Select(["data", "email"], Var("doc"))),
LowerCase(Var("searchTerm"))
)
)
)
)
),
Lambda("ref", Get(Var("ref")))
)
)
10.2 产品筛选 #
javascript
// 产品筛选查询
Let(
{
category: "electronics",
minPrice: 100,
maxPrice: 1000,
brand: "apple"
},
Map(
Filter(
Paginate(Match(Index("products_by_category"), Var("category"))),
Lambda("ref",
Let(
{ doc: Get(Var("ref")) },
And(
GTE(Select(["data", "price"], Var("doc")), Var("minPrice")),
LTE(Select(["data", "price"], Var("doc")), Var("maxPrice")),
Equals(Select(["data", "brand"], Var("doc")), Var("brand")),
Equals(Select(["data", "status"], Var("doc")), "active")
)
)
)
),
Lambda("ref", Get(Var("ref")))
)
)
10.3 订单报表 #
javascript
// 订单统计报表
Let(
{
startDate: Time("2024-01-01T00:00:00Z"),
endDate: Time("2024-12-31T23:59:59Z"),
orders: Paginate(
Range(
Match(Index("orders_by_date")),
[Var("startDate")],
[Var("endDate")]
)
)
},
{
totalOrders: Count(Var("orders")),
totalRevenue: Reduce(
Map(
Var("orders"),
Lambda("ref", Select(["data", "total"], Get(Var("ref"))))
),
Lambda(
["acc", "val"],
Add(Var("acc"), Var("val"))
),
0
)
}
)
十一、总结 #
基础查询要点:
| 操作 | 说明 |
|---|---|
| Match | 索引匹配查询 |
| Filter | 条件过滤 |
| Map | 映射转换 |
| Lambda | 匿名函数 |
| Union | 并集 |
| Intersection | 交集 |
| Difference | 差集 |
| Range | 范围查询 |
下一步,让我们学习索引查询!
最后更新:2026-03-27