简体   繁体   中英

ArangoDB - how find element in collection by substring of element of array?

There is a collection with this structure:

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

I need to find an element of this collection, in the key data has a value that contains a string that contains the substring xga5fd7h68745v46ed2

Can I write this query? How will it look like?

You can loop over the array items like over any other list:

FOR d IN collection_name
FILTER IS_LIST(d.data)
FOR data IN d.data
FILTER CONTAINS(data, "xga5fd7h68745v46ed2")
RETURN d

If your data array has multiple occurances of substring you are searching for and want to return only the unique documents that contains search value, then use query like this:

// 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

Query which pluma suggested might do the job for your usecase if values you are looking for are all unique for all data array elements. If there are multiple occurances of substrings you search for within data array elements then the query will return duplicate documents. For example consider this example dataset:

[
  {
    "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"
    ]
  }
]

If you use this query

FOR d IN collection_name
    FILTER IS_LIST(d.data)
    FOR data IN d.data
        FILTER CONTAINS(data, "ab")
        RETURN d

It will return 4 documents even when the collection contains only 2. However the very first query in this post will return only 2. To be clear I'm not saying that pluma's solution is wrong it just depends what do you expect to be returned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM