3

有一个具有这种结构的集合:

{
   "user": 1, 
   "timestamp": 216354113151, 
   "message": "asddsaasddsaa", 
   "data": [
     "name = Jack & hash = s5w464t35w145df13s5df4sdg & id = 2" 
     "name = Mansonack & hash = xga5fd7h68745v46ed2 & id = 18" 
   ] 
}

我需要找到这个集合的一个元素,在关键数据中有一个值包含一个包含子字符串 xga5fd7h68745v46ed2 的字符串

我可以写这个查询吗?它会是什么样子?

4

2 回答 2

2

您可以像遍历任何其他列表一样遍历数组项:

FOR d IN collection_name
FILTER IS_LIST(d.data)
FOR data IN d.data
FILTER CONTAINS(data, "xga5fd7h68745v46ed2")
RETURN d
于 2014-10-08T09:11:32.150 回答
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 的解决方案是错误的,它仅取决于您期望返回什么。

于 2014-10-08T09:49:33.720 回答