Debaditya Mandal
Debaditya Mandal

Reputation: 33

How to prevent data duplication in redisgraph?

I wrote one code to store graph in redisgraph. Initially it is storing single graph but if I execute the same code second time then it is storing the same graph in database without replacing the previous graph.So, now I am getting two same graphs in a single key in the database.I don't want any duplicate graph or any duplicate node that means if I execute same code again it should replace previous graph.How will I do that?

Upvotes: 1

Views: 1043

Answers (3)

vinyll
vinyll

Reputation: 11429

MERGE will do like a find or create. If your node, edge or path does not exist it will create it.

That's the recommended way to avoid duplicate entities if they are not permitted.

Upvotes: 0

Jagadeesh Urlana
Jagadeesh Urlana

Reputation: 87

You can Use MERGE clause to prevent inserting duplicate data.

Below is the query to remove duplicate records from existing data

MATCH (p:LabelName)
WITH p.id as id, collect(p) AS nodes 
WHERE size(nodes) >  1
UNWIND nodes[1..] AS node
DELETE node

Upvotes: 0

sentientcabbage
sentientcabbage

Reputation: 466

If your code consists of a series of CREATE commands (whether through Cypher or one of the RedisGraph clients), running it twice will duplicate all of your data. This is not to say that the key stores two graphs; rather, it is one graph with every entity repeated.

If you would like to replace an existing graph, you should delete the existing graph first. You can delete a graph using a Redis command:

DEL [graph key]

Or a RedisGraph command:

GRAPH.DELETE [graph key]

The two are functionally identical.

Conversely, if you want to update an existing graph without introducing duplicates, you should use the MERGE clause as described in the RedisGraph documentation.

Upvotes: 4

Related Questions