如果您的数据数组多次出现您要搜索的子字符串,并且只想返回包含搜索值的唯一文档,则使用如下查询:
// what you are looking for in string
LET searchValue = 'xga5fd7h68745v46ed2'
// go through items of your collection
FOR item IN collection_name
// store result of subquery in temporary variable
LET wasItemFound = FIRST((
// go through elements of your data array
FOR str IN item.data
// search for desired value within the element
FILTER CONTAINS(str, searchValue)
// we need to find only single occurance of searchValue
LIMIT 1
// return true if it was found
RETURN true
))
// give me only items which contained value you searched for
FILTER wasItemFound == true
RETURN item
如果您要查找的值对于所有数据数组元素都是唯一的,则查询哪个 pluma 建议可能会为您的用例完成这项工作。如果您在数据数组元素中搜索的子字符串多次出现,则查询将返回重复文档。例如考虑这个示例数据集:
[
{
"user": 1,
"data": [
"name = Jack & hash = abc & id = 2",
"name = Mansonack & hash = abcd & id = 18"
]
},
{
"user": 2,
"data": [
"name = Jack & hash = abb & id = 2",
"name = Mansonack & hash = abc & id = 18",
"name = xxx& hash = aaa & id = 18"
]
}
]
如果您使用此查询
FOR d IN collection_name
FILTER IS_LIST(d.data)
FOR data IN d.data
FILTER CONTAINS(data, "ab")
RETURN d
即使集合仅包含 2 个文档,它也会返回 4 个文档。但是,本文中的第一个查询将仅返回 2 个。要明确的是,我并不是说 pluma 的解决方案是错误的,它仅取决于您期望返回什么。