假设您有一个包含带有价格字段的文档的集合,我们称它为 ... ermm ... priceables!
让我们将价格为 1 和价格为 2 的两个文档添加到集合中。
[{
"price": 1
},
{
"price": 2
}]
Image in UI console of the documents
创建常规索引
使用以下 sn-p 在该价格值上创建一个常规范围索引:
CreateIndex({
name: "regular-value-index",
unique: false,
serialized: true,
source: Collection("priceables"),
terms: [],
values: [
{
field: ["data", "price"]
}
]
})
创建反向索引
可以有多个值(例如复合索引),并且可以为每个值设置反向字段。
要创建反向索引,请将该特定值的 reverse 设置为 true。
CreateIndex({
name: "reverse-value-index",
unique: false,
serialized: true,
source: Collection("priceables"),
terms: [],
values: [
{
field: ["data", "price"],
reverse: true
}
]
})
如果您转到 UI 控制台并打开索引,您会注意到值从高到低排序。
UI console, reverse index
我认为让您感到困惑的是您还不能在 UI 中设置反向布尔值。但是你可以直接去 Shell 并粘贴 FQL 代码来创建索引:Image of the shell