2

Is there a way to update an existing attribute in a node to a new value after a specific time period passes ?

For example: Node 'Offer' has the below attributes

  1. Offer Name (String)
  2. Offer ID (Numeric)
  3. CREATED_ON (Timestamp)
  4. IS_VALID ('Yes'/'No')

Process:

  1. When an initial "Offer" is created IS_VALID value is "Yes"
  2. After 7 days of creation IS_VALID value should be set to "No" automatically for each of the offers

I know that there is TTL , but I don't want my node to be deleted after 7 days, rather the attribute to be updated to "No" from "Yes"

Is there any way I can achieve this in Neo4j?

4
  • What about not storing this is_valid property and let the client application decide? Is that an option? Commented Jan 26, 2021 at 10:03
  • @fbiville It might be an inefficient approach, imagine fetching the non-valid offers everytime. Commented Jan 26, 2021 at 16:15
  • 1
    Let me rephrase: let the application run (in pseudo-Cypher) MATCH (offer:Offer) WHERE TIMESTAMP() < offer.created_on + 7 days RETURN offer. The application fetches only the valid offers that way and you do not need to store any is_valid field for that. Commented Jan 26, 2021 at 21:05
  • 1
    @fbiville - not storing is_valid was the first option I considered, but this attribute is also being used for some other purposes and it's just not for displaying offers on a page, hence couldn't use this way Commented Jan 27, 2021 at 1:19

1 Answer 1

1

You can use apoc procedure calls for scheduling background jobs. apoc.periodic.schedule() makes sense in your case.

CALL apoc.periodic.schedule('offerValidater', "MATCH (n: Offer) WHERE apoc.date.currentTimestamp() - n.CREATED_ON > 604800000 SET n.IS_VALID = 'No'", 86400) 

Here I assumed that offer.CREATED_ON is in epoch milliseconds. If you are using other format timestamps you need to do a comparison accordingly. offerValidater routine is scheduled to run with a delay of 86400 seconds (1 day).

Sign up to request clarification or add additional context in comments.

3 Comments

thank you for the suggestion, this looks feasible but there is a catch here. If the new offer is created on Jan-27-2021 10:15, the offer should be valid only until Feb-3-2021 10:15. Delay of 1 day might not be feasible here and I would have to go for an hourly delay. And even then there is a chance that some of the offers might be valid for more than 7 days.
@Vader.219k Yes of course. If we are adding offers at the same time every day then 24 hrs thing works. If you want to have it in real-time doing it from the client's side is good. Maybe every time you read Offer data, you update the entries and return the fresh ones. This again has its cons. You need every client reading the offer data perform such a query.
@Vader.219k In that case, you can have another field valid_time, and do apoc.date.currentTimestamp() - n.CREATED_ON > n.valid_time

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.