Skip to content

Instantly share code, notes, and snippets.

@paulozullu
Last active January 13, 2020 18:01
Show Gist options
  • Save paulozullu/ae7872a025f84ba355fa9f13a8ae259d to your computer and use it in GitHub Desktop.
Save paulozullu/ae7872a025f84ba355fa9f13a8ae259d to your computer and use it in GitHub Desktop.
Neo4j

1 - Get node by ID

MATCH (s)
WHERE ID(s) = 65110
RETURN s

2 - Add attribute to the existing relationships (probability=1 or 100%)

MATCH()-[r:relationship]->()
SET r += { new_attrib: value}

3 - Rename node label

MATCH (n:OLD_LABEL)
REMOVE n:OLD_LABEL
SET n:NEW_LABEL

4 - Get the users that lives in a radius of a maximum of 10km of distance from a city

MATCH (co:Coordinate)-[:COORDINATE_OF]->(l:Location)<-[:LIVES_IN]-(u:User)
MATCH (sp:Location {name: 'CITY_NAME'})<-[:COORDINATE_OF]-(sp_coordinate: Coordinate)
WITH toFloat(sp_coordinate.lat) AS sp_lat, toFloat(sp_coordinate.lon) AS sp_lon, toFloat(co.lat) AS latitude, toFloat(co.lon) AS longitude, l.name AS location, u.username AS user
WHERE 2 * 6371 * asin(sqrt(haversin(radians(sp_lat - latitude))+ cos(radians(sp_lat))*
  cos(radians(latitude))* haversin(radians(sp_lon - longitude)))) < 50 RETURN DISTINCT location, user,  2 * 6371 * asin(sqrt(haversin(radians(sp_lat - latitude))+ cos(radians(sp_lat))*
  cos(radians(latitude))* haversin(radians(sp_lon - longitude)))) + ' Km' AS Distance

5 - Rename relationships and keep the same properties and properties values

MATCH (n:User {name:"foo"})-[r:REL]->(m:User {name:"bar"})
CREATE (n)-[r2:NEWREL]->(m)
// copy properties, if necessary
SET r2 = r
WITH r
DELETE r

6 - Delete all node and relations

MATCH (n)
DETACH DELETE n

7 - Import CSV

LOAD CSV WITH HEADERS FROM 'file:///PATH_TO_CSV/CSV_FILE.csv' AS line
MERGE (t:Node1 {_id:line._id})
ON CREATE SET t.property1=line.field1, t.property2=line.field2, t.property3=line.field3
MERGE (c:Node2 {name: line.field1})
MERGE (s:Node3 {name: line.field2})
CREATE (c)-[:PART_OF]->(t)
CREATE (s)-[:PART_OF]->(t)

8 - Create index

CREATE INDEX ON :LabelName(propertyKey)

9 - Check created indexes

CALL db.indexes()

10 - Get random nodes

MATCH (u:User)
WITH u, rand() AS number
RETURN u
ORDER BY number
LIMIT 100

11 - Case insensitive search

MATCH (n:Node) WHERE n.name =~ '(?i)pAuLO' RETURN n.name
>>> Paulo

12 - Remove duplicated nodes

MATCH (c:CategoryInterest) WHERE c.category=' EVENTOS'
WITH c.category AS category, COLLECT(c) AS nodelist, COUNT(*)
AS count
WHERE count > 1
CALL apoc.refactor.mergeNodes(nodelist) YIELD node
RETURN node

13 - Removing first 50 nodes

MATCH (c:CommunityWopik) WHERE NOT EXISTS(c.social_media) 
WITH (COLLECT(c))[0..] AS wopiks UNWIND wopiks AS wopik DETACH DELETE wopik

14 - Get nodes without relationship

MATCH (c:MyNode) WHERE NOT (c)-[]-() AND c.category='test' RETURN c

15 - Remove property from a node

MATCH (a { name: 'Andy' })
REMOVE a.age
RETURN a.name, a.age

16 - Remove duplicated relationships

MATCH (a)-[r]->(b)
WITH a, b, TAIL (COLLECT (r)) as rr
FOREACH (r IN rr | DELETE r)

17 - Create new relationship where it has a property equal as it maximum plus 1

MATCH (p:Post)-[r:category]->(c:Cat)
WHERE p.post=1
WITH p,MAX(r.max) AS r_max
MATCH (c1:Cat) WHERE c1.cat="produtos"
WITH p, r_max, c1
MERGE (p)-[:category {max: r_max+1}]->(c1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment