DB - GraphDB (neo4j & cypher)

cypher 쿼리 사용법 정리

특정 값을 가진 노드를 찾기

  • MATCH (SQL: SELECT)
    • (variable: Label {property : value})-[:relationshop]->(variable: Lable)
  • OPTIONAL MATCH
    • outer join과 같은 기능으로 특정 property가 없는 node도 ‘null’ 값으로 출력함
  • [ex] Category(label)에서 name(property)이 “스킨케어”인 노드를 찾기
match (cat:Category {name : "스킨케어"})
return cat
{"identity":0,
	"labels" : ["Category"],
	"properties" : {
			"name" : "스킨케어"
			}
}
  • [ex] Category(label) name(property)이 “스킨케어”인 Product(label)의 노드를 찾기
match (cat:Category {name : "스킨케어"})<-[:classified]-(prd:Product)
return prd
{"identity":0,
	"labels" : ["Category"],
	"properties" : {
			"name" : "스킨케어"
			}
}

연산자를 사용해 특정 값을 가진 노드를 찾기

  • WHERE
    • 부정: NOT
    • 값의 대소 비교: <, >, <=, >=
    • 값이 존재: exists()
    • string 연산자: STARTS WITH, CONTAINS, ENDS WITH
    • 리스트 요소 IN

Aggregation

  • count()
    • 노드 개수를 반환
    • count(property) : null 값을 제외하고 카운트
    • count(*) :  null 값을 포함해 카운트
  • collect()
    • 노드를 list에 담기
    • collection 요소 개수 : size()
  • UNWIND
    • list의 요소를 풀기
  • ORDER BY
    • 특정 property를 기준으로 오름차순(default) 또는 내림차순(ORDER BY ... DESC)
  • DISTINCT
    • 값의 중복 제거
  • [ex] Unwind 사용법
    WITH ['Graphs','Query Languages'] AS techRequirements
    UNWIND techRequirements AS technology
    

Reference

official-search

official-aggregation