如果你的數據數組的子串,你正在尋找,並希望只返回包含搜索值的唯一文檔的多個occurances,然後使用這樣的查詢:
// 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
它將返回4號文件,即使集合只包含2但是,在這個職位的第一個查詢只會返回2.要明確我並不是說pluma的解決方案是錯誤的,而僅僅取決於你期望得到的結果。
非常感謝!:) – jonua 2014-10-08 09:21:15