简体   繁体   中英

How to Loop Over Date Range in ArangoDB

I need to return a value for every date between a start and end date.

For each row : if the date exists in row.date , then I need to return row.myValue else I need to return null.

Example:

dateRange = {start: '2018-01-01', end: '2018-01-03'}
aqlCollection = [
  {date: '2018-01-01', myVal: 1},
  {date: '2018-01-02', myVal: 2},
  {date: '2017-05-18', myVal: 3}
]

This should return:

[
  {'2018-01-01': 1},
  {'2018-01-02': 2},
  {'2018-01-03': null}
]

This can be accomplished simply if there was a WHILE loop in arangodb. Or if I could for loop using an incrementer instead of doing FOR date IN dates then I could just say FOR date=startRange; date<endRange; date+=24hrs FOR date=startRange; date<endRange; date+=24hrs FOR date=startRange; date<endRange; date+=24hrs .

Any ideas on how this can be achieved within an Arango query? If it is not possible I will do an O(n) loop over after a simple group by date query and add in all the dates that do not exist.

Please take a look at: https://docs.arangodb.com/3.4/AQL/Functions/Date.html .

If you have an attribute in the date format then you can enumerate the collection's items using a for-loop and use a filter on the attribute to get the dates in a certain range. As you already showed. Adding an index on the attribute will give you a nice speed up. It will make sure that only objects with matching dates will be inspected.

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