任何人都知道如何创建一个索引,其中术语是一个 ref 并且可以通过数组中的嵌套对象进行搜索?
在“对话”集合中,我保存了以下示例数据:
{
"created": Time("2019-09-29T22:11:01.493034Z"),
"updated": Time("2019-09-29T22:11:01.493034Z"),
"participants": [
{
"ref": Ref(Collection("users"), "244754936642929163"),
"firstname": "John",
"creator": true
},
{
"ref": Ref(Collection("users"), "244517629884105216"),
"firstname": "Max"
}
]
}
最好有一个索引,我可以在其中搜索参与者数组中是否包含 ref。
最佳答案
这可以使用索引来解决,并将数组选为术语。当作为术语的目标字段是数组时,将为数组中的每个 项生成一个单独的索引条目。所以假设现存集合 parent
和 child
然后
db> CreateIndex({name: "cs", source: Collection("parent"), terms: [{field: ["data", "child"]}]})
{
ref: Index("cs"),
ts: 1569831659780000,
active: true,
serialized: true,
name: 'p',
source: Collection("parent"),
terms: [ { field: [ 'data', 'child' ] } ],
partitions: 1
}
会做生意。用法示例:
db> Create(Collection("child"), {})
{
ref: Ref(Collection("child"), "244918886014648845"),
ts: 1569831701200000
}
db> Create(Collection("child"), {})
{
ref: Ref(Collection("child"), "244918887478460941"),
ts: 1569831702590000
}
db> Create(Collection("parent"), {data:{child:[Ref(Collection("child"), "244918886014648845"), Ref(Collection("child"), "244918887478460941")]}})
{
ref: Ref(Collection("parent"), "244918956982272520"),
ts: 1569831768880000,
data: {
child: [
Ref(Collection("child"), "244918886014648845"),
Ref(Collection("child"), "244918887478460941")
]
}
}
db> Paginate(Match(Index("cs"), Ref(Collection("child"), "244918886014648845")))
{
data: [
Ref(Collection("parent"), "244918956982272520")
]
}
任何一个子引用都会匹配。
关于faunadb - 具有可搜索嵌套对象数组的 CreateIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58168598/