有一个具有这种结构的集合:
{
"user": 1,
"timestamp": 216354113151,
"message": "asddsaasddsaa",
"data": [
"name = Jack & hash = s5w464t35w145df13s5df4sdg & id = 2"
"name = Mansonack & hash = xga5fd7h68745v46ed2 & id = 18"
]
}
我需要找到这个集合的一个元素,在密钥数据中有一个值,其中包含一个包含子字符串的字符串xga5fd7h68745v46ed2
我可以写这个查询吗?它会是什么样子?
答案 0 :(得分:2)
您可以遍历任何其他列表中的数组项:
FOR d IN collection_name
FILTER IS_LIST(d.data)
FOR data IN d.data
FILTER CONTAINS(data, "xga5fd7h68745v46ed2")
RETURN d
答案 1 :(得分:1)
如果您的数据数组有多个子字符串,您正在搜索并希望仅返回包含搜索值的唯一文档,则使用如下查询:
// 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的解决方案是错误的取决于你期望什么回报。