cancel
Showing results for 
Search instead for 
Did you mean: 

Creating Relationship basis condition and limit

pragyasood28
Node Clone

Hi All,
I am trying to create a relationship between the below path where all nodes have a date property to other nodes present in my Graph DB with rest of the dates like '2021-05-01' , '2021-06-01' ,,'2021-07-01'etc. but the relationship should be created where every month date is connected to the next month date

I have written this query : Match (a:Storage),(b:Storage)
where b.date=a.date+duration('P1M')
create (a)-[r:impact]->(b)

This works fine and creates the relation between all nodes but I just want to create relation between the nodes shown in the picture to just their next month i.e in the picture date is '2021-04-01' (but it can be any date) so it should be connected to nodes with date '2021-05-01' only and not connected further to '2021-06-01'. I want to limit my relationship creation. Is it possible ? And how to achieve it .

This is the Output that I want

Thanks

7 REPLIES 7

SamChalvet
Node Clone

Hi, if the Storage node that you are trying to connect too already exists you should be able to do something like this:

MATCH (a:Storage)
MATCH (b:Storage {date: a.date+duration('P1M')})
MERGE (a)-[:IMPACT]->(b)
RETURN *

I hope that helps.

Hi Sam,

I just tried this what you suggested so this is also creating the relationship between all nodes all dates basis 1 month gap like '2021-04-01' connected to '2021-05-01' and then to '2021-06-01' and so on till '2023-03-01' (max date in my DB). I am getting this

But I just want to stop the relationship creation after 1 relationship between '2021-04-01' and '2021-05-01' .

Can you suggest how to achieve this.

Thanks

Ah I see.

Could this be solved then by limiting the a:Storage node that is returned based on the pattern like so:

MATCH ()-[:TRANSPORTED]->(a:Storage)
MATCH (b:Storage {date: a.date+duration('P1M')})
MERGE (a)-[:IMPACT]->(b)
RETURN *

That way your "a" node will always be one that has been TRANSPORTED.

Thanks Sam, I can make use of your and Benoit's query. This was helpful.

pragyasood28
Node Clone

So I cannot create it by limiting a:Storage basis TRANSPORTED because the other month Storage nodes like '2021-06-01', '2021-07-01' etc also have TRANSPORTED relationship. Sorry, I didn't mention this before that those months are also separate paths with TRANSPORTED Relation that I have in my DB.

Benoit_d
Graph Buddy

One trouble is that in order to have only one relatioship "IMPACT" you should be able to query what you are creating, which is not possible within one query.
A simple solution, even if not the prettiest, would be to run a second query to delete the useless relationship in a chain:

MATCH (a:Storage)-[:IMPACT]->(b:Storage)-[rel:IMPACT*]->(c:Storage)
DELETE rel

Thanks Beniot this is helpful.