简体   繁体   中英

Sorting on _key and filter

I have a requiremnt in which I am getting stucked. Below are few points that needs to be done.

1: I want to sort on _key field which is system generated (configured as auto increment) and filter them accordingly. Eg: 
SORT users._key DESC
FILTER TO_NUMBER(users._key) > 1999968
The problem here is _key is string and I have to convert _key TO_NUMBER which doesn't seems to be a good approach, is there are way to correct it.

FILTER users._key > '1999968' is not giving correct results.
2: How to use grouping with views I need to group few records but it shows be error everytime I place COLLECT keyword.
eg: 
for suser in sortedUsers <----- view
SEARCH suser ._to == user._id <------ some searching (not actual code)
SORT suser ._key DESC <----------- sorting
FILTER suser ._key > 1999968 <-------- filtering
COLLECT temp = suser.country <---------- this is sample command not actual
return temp

But my question is where does the collect command goes when we use views.Even DINSTINCT  will work but how to sync it with complex queries that return complex results.

I ran into the same issue. In Javascript we see the same:

"2" > "13" -> true
"2" >  13  -> false
 2  > "13" -> false

So comparing numerical string values will not work (because strings are not numbers). To compare numerical strings, a conversion must be done to numerical values.

I don't know "the answer" to your question because it depends on optimalizations done by ArangoDB. But you can try different options for the conversion and benchmark the query with the Profile and Explain functions in the web interface. The result might differ depending on the exact query you do.

For example:

FOR d IN CollectionName
  FILTER d._key > 1999968 // implicit conversion to number
  RETURN d

FOR d IN CollectionName
  FILTER d._key - 1999968 > 0 // another implicit conversion to number
  RETURN d

FOR d IN CollectionName
  FILTER +d._key > 1999968 // explicit conversion of key to number
  RETURN d

FOR d IN CollectionName
  FILTER TO_NUMBER(d._key) > 1999968 // use ArangoDB's conversion function
  RETURN d

The first implicit conversion d._key > 1999968 seems to work well for me.

By the way... sorting might have the same issue. Eg

SORT d._key DESC
SORT TO_NUMBER(d._key) DESC

might result in different output.

So maybe it's better to add an indexed numerical property to the document that can be filtered and sorted without any tricks.

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