Lionel Cichero
Lionel Cichero

Reputation: 563

Neo4j: Is there a way to create a new node based on a group of nodes that meet certain criteria

I have a neo4j database up and running. I also have a process that runs every 5 minutes and what it does is to create nodes of type "Point".

"Point" has the following properties:

pointId, cameraId, classId, groupId, datetime

Nodes of type "Point" relate to themself if: pointId & cameraId & classId & groupId are the same.

Is it possible to somehow get all the nodes "Point" that relate themself and based on that group of nodes create a new node "Line" where "Line"-[:CONTAINS]->"Point"?

UPDATE: Following image shows what I have and what I need. For simplicity, I've just defined the property "camera", if a node Point shares the camera then it needs to be grouped.

enter image description here

Upvotes: 1

Views: 296

Answers (1)

Rajendra Kadam
Rajendra Kadam

Reputation: 4052

Yes, it's possible.

You need to collect all the points for each pair of these properties. Then create the Line node and then create a relationship between the created line and all the grouped points.

Add the required properties to the line node in the following query.

MATCH (p:Point)
WITH p.pointId as pointId, p.cameraId as cameraId, p.classId as classId, p.groupId as groupId, collect(p) as related_points
CREATE (line:Line)
WITH line, related_points
UNWIND related_points as point
CREATE (line)-[:CONTAINS]->(point)

Upvotes: 1

Related Questions