Cypher Filter by Date .... WHERE x.birth_date > "10/06/1947"

6,231 views
Skip to first unread message

Rich Westington

unread,
Dec 29, 2011, 7:54:58 PM12/29/11
to ne...@googlegroups.com
How would I be able to filter all people in a graph who were born before 10/06/1947 ?
Right now I'm storing birthdays as a String property called birth_date with format MM/DD/YYYY

    START n=node(2) MATCH (n)<-[:IS_A]-(x) WHERE x.birth_date > "10/06/1947" RETURN x.name,x.birth_date ORDER BY x.birth_date DESC

Is there any known ways of filtering by date without using UNIX Timestamps because I have many dates before 1970.

Andres Taylor

unread,
Dec 30, 2011, 4:16:04 AM12/30/11
to ne...@googlegroups.com
On Fri, Dec 30, 2011 at 1:54 AM, Rich Westington <richwes...@gmail.com> wrote:
How would I be able to filter all people in a graph who were born before 10/06/1947 ?
Right now I'm storing birthdays as a String property called birth_date with format MM/DD/YYYY

    START n=node(2) MATCH (n)<-[:IS_A]-(x) WHERE x.birth_date > "10/06/1947" RETURN x.name,x.birth_date ORDER BY x.birth_date DESC

Neo4j doesn't support dates very well today. We have plans to work on it, but I don't know when that might happen.

Does anyone have a good work-around for this that they can share?

Andrés

Newbie

unread,
Jan 11, 2012, 5:55:31 PM1/11/12
to Neo4j
Anyone has more update on this? In my case, dates are very important.

On Dec 30 2011, 4:16 am, Andres Taylor <and...@neotechnology.com>
wrote:
> On Fri, Dec 30, 2011 at 1:54 AM, Rich Westington
> <richwesting...@gmail.com>wrote:

Nigel Small

unread,
Jan 11, 2012, 6:09:22 PM1/11/12
to ne...@googlegroups.com
I have always found storing dates in ISO format "YYYY-MM-DD" allows textual sorting to become infinitely easier. There are also many libraries which can support this format so it's very easy to work with.

Nige

Peter Neubauer

unread,
Jan 11, 2012, 6:16:39 PM1/11/12
to ne...@googlegroups.com

Well,
I have been storing dates as longs on the nodes and then converted them from and to formatted strings outside the Cypher query. Would that work in your case?

/peter

Sent from my phone, please excuse typos and autocorrection.

xia.in...@gmail.com

unread,
Jan 11, 2012, 6:26:09 PM1/11/12
to ne...@googlegroups.com
That's a good suggestion! 

A question is how would you query something like "give me something prior to this date".

Thanks,

xia.in...@gmail.com

unread,
Jan 11, 2012, 6:28:55 PM1/11/12
to ne...@googlegroups.com
How do you handle for dates that are prior to the epoc time? As long as I could do a query using something as in Rich's original post, i.e. using x.birth_date > a_number_equivalent_to_a_date, I think I can live with that for now. 

Thanks

Rich Westington

unread,
Jan 11, 2012, 6:30:39 PM1/11/12
to ne...@googlegroups.com
Do you mean to do something like this?

if date is 2012-01-11 19:26:02

    long birth_date = 20120111192602

then in cypher ... to get all dates before today

    WHERE x.birth_date < 20120111000000

I never thought about doing it like that...

Nigel Small

unread,
Jan 11, 2012, 6:58:55 PM1/11/12
to ne...@googlegroups.com
Yes, you can either use a numeric (20120111) or textual ("2012-01-11") version. Given that the ISO date format (http://en.wikipedia.org/wiki/ISO_8601describes the latter, that's generally my preference unless I'm specifically limited to numeric values.

You can then of course compare, so:

x <= "2011-12-31"

would give you everything on or before the end of 2011, for example.

This isn't Cypher/Neo specific of course - I've used it on many DB platforms.

Nige

Rich Westington

unread,
Jan 11, 2012, 7:02:58 PM1/11/12
to ne...@googlegroups.com
Do you know if using a long would be faster than strings when using ORDER BY in neo4j or are they about the same?

Tero Paananen

unread,
Feb 15, 2012, 2:58:09 PM2/15/12
to Neo4j
I just ran into this issue.

I'm using SDN with NodeEntity objects that have properties of type
java.util.Date.

Those fields get saved in Neo4J as strings.

So now I have Repository methods that look like this:

@Query(value = "start user = node({nodeId}) match (user)<-
[cn:CONNECTED]-(c) where cn.contactDate > {since} return c", type =
QueryType.Cypher)
Page<User> getConnectedSince(@Param("nodeId") Long nodeId,
@Param("since") String since, Pageable pageable);

And I call it with something like:

Iterable<User> connections = getConnectedSince(1l, String.valueOf(new
Date().getTime()), pageable);


While that's working fine, it just seems really, really dumb given
that Date is natively representated as a long, and numeric comparisons
should be much more effective than string comparisons.

Migrating Dates from string to long representation in SDN would break
all existing implementations, however (unless they could be converted
"on-the-fly" somehow).

Michael, what do you think?

-TPP

James Thornton

unread,
Feb 15, 2012, 3:50:30 PM2/15/12
to ne...@googlegroups.com
See...

"What every programmer should know about time" (http://unix4lyfe.org/time/)

- James

Tero Paananen

unread,
Feb 15, 2012, 4:32:35 PM2/15/12
to ne...@googlegroups.com
On Wed, Feb 15, 2012 at 3:50 PM, James Thornton
<james.t...@gmail.com> wrote:
> See...
>
> "What every programmer should know about time" (http://unix4lyfe.org/time/)

Quoteth:

"When storing time, store Unix time. It's a single number. "

-TPP

Michael Hunger

unread,
Feb 15, 2012, 4:41:48 PM2/15/12
to ne...@googlegroups.com
Tero,

can you please raise an issue about this.

Thanks a lot

Michael

Luanne Coutinho

unread,
Feb 15, 2012, 10:58:00 PM2/15/12
to ne...@googlegroups.com
I just store it as a long, convert my target date as well to a long and compare those- I believe dates before the epoch should still work because the long just turns to negative prior to that. Will double check though now that it's been brought up.
Reply all
Reply to author
Forward
0 new messages