Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
What if we want to filter our articles based on these temporal values.
Let’s start by finding the articles that were published on 1st June 2019. The following query does this:
MATCH (article:Article)
WHERE article.datePublished = date({year: 2019, month: 6, day: 1})
RETURN article.title AS title,
article.created AS created,
article.datePublished AS datePublished,
article.readingTime AS readingTime
What about if we want to find all the articles published in June 2019? We might write the following query to do this:
MATCH (article:Article)
WHERE article.datePublished = date({year: 2019, month: 6})
RETURN article.title AS title,
article.created AS created,
article.datePublished AS datePublished,
article.readingTime AS readingTime
If we run this query we’ll get the following results:
This doesn’t seem right - what about the Cypher Basics II
article that was published on 2nd June 2019?
The problem we have here is that date({year: 2019, month:6})
returns 2019-06-01
, so we’re only finding articles published on 1st June 2019.
We need to tweak our query to find articles published between June 1st 2019 and July 1st 2019. The following query does this:
MATCH (article:Article)
WHERE date({year: 2019, month: 7}) > article.datePublished >= date({year: 2019, month: 6})
RETURN article.title AS title,
article.created AS created,
article.datePublished AS datePublished,
article.readingTime AS readingTime
What about if we want to filter based on the created
property, which stores Datetime
values?
We need to take the same approach when filtering Datetime
values as we did with Date
values.
The following query finds the articles created after July 2019:
MATCH (article:Article)
WHERE article.created > datetime({year: 2019, month: 7})
RETURN article.title AS title,
article.created AS created,
article.datePublished AS datePublished,
article.readingTime AS readingTime
And finally filtering durations. We might be interested in finding articles that can be read in 3 minutes or less.
We’ll start with the following query:
MATCH (article:Article)
WHERE article.readingTime <= duration("PT3M")
RETURN article.title AS title,
article.created AS created,
article.datePublished AS datePublished,
article.readingTime AS readingTime
If we execute that query we’ll see the following output:
Neo.ClientError.Statement.SyntaxError: Type mismatch: expected Float, Integer, Point, String, Date, Time, LocalTime, LocalDateTime or DateTime but was Duration (line 2, column 29 (offset: 52))
"WHERE article.readingTime < duration("PT3M")"
^
If we want to compare durations we need to do that comparison by adding those durations to dates. We don’t really care about dates for our query so we’ll just use the current time to work around this issue. We can get the current time by calling the datetime() function.
Our updated query reads like this:
MATCH (article:Article)
WHERE datetime() + article.readingTime <= datetime() + duration("PT3M")
RETURN article.title AS title,
article.created AS created,
article.datePublished AS datePublished,
article.readingTime AS readingTime