cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Creating date nodes by looping through a start date and an end date

So I want to create 'n' number of nodes for 'n' number of days starting from a fixed start date (say 2020-01-01) to a fixed end date (current day).

How can I do this in a smart way (by using some kind of loop). I tried using UNWIND, but that seems to need me to create a list of all date first and then iterate through that. I want to avoid doing that kind of manual work.

Thanks!

============================================================================
Please keep the following things in mind:

  1. did you search for what you want to ask before posting?
  2. please use tags for additional info
  3. use a self-descriptive title

Please format code + Cypher statements with the code </> icon, it's much easier to read.

Please provide the following information if you ran into a more serious issue:

  • neo4j version, desktop version, browser version
  • what kind of API / driver do you use
  • screenshot of PROFILE or EXPLAIN with boxes expanded (lower right corner)
  • a sample of the data you want to import
  • which plugins / extensions / procedures do you use
  • neo4j.log and debug.log
1 ACCEPTED SOLUTION

I got how to do this. I simply loaded a csv with all the dates in it.

However I would still love to know if there is a way to do it using ranges or something!

View solution in original post

3 REPLIES 3

I got how to do this. I simply loaded a csv with all the dates in it.

However I would still love to know if there is a way to do it using ranges or something!

What do you mean by this? It's not manual work, and it is one of only two ways to efficiently handle lists. Avoiding UNWIND and collect is not a viable strategy in Neo4j, or any graph database.

That said, I'll admit that I struggled most with understanding those, than almost anything else, when I started learning Neo4j and Cypher. My personal favorite thread about it:
Cypher grammer, syntax, and documentation -- List, Collect, avg, etc are impenetrable to me.

Your problem should be solved with creating, and unwinding, a list

Also, date, datetimes, and durations are similarly confusing. Here's some useful references for working with date and times:

WITH date("1985-01-01") AS startDate, date("1985-01-20") AS endDate
WITH startDate, endDate, range(0, duration.inDays(startDate, endDate).days) as dayDiffList
UNWIND dayDiffList AS dayDiff
WITH (startDate + duration({days: dayDiff})) as date RETURN date

...that will get you a list of dates, with which you can do whatever you want, like create nodes, and query or order results by date.

WITH date("1985-01-01") AS startDate, date("1985-01-20") AS endDate
WITH startDate, endDate, range(0, duration.inDays(startDate, endDate).days) as dayDiffList
UNWIND dayDiffList AS dayDiff
WITH (startDate + duration({days: dayDiff})) as dateVal
MERGE (:DateEntry {date: dateVal})
MATCH (d:DateEntry) 
WHERE d.date > date("1985-01-5")
RETURN d.date ORDER BY d.date DESC

Wow that's great! Thanks for this, will try it out!