美文网首页
Nosql-neo4j-Cypher 语法(3)

Nosql-neo4j-Cypher 语法(3)

作者: Viterbi | 来源:发表于2022-09-15 08:57 被阅读0次

目录: [TOC]

时间日期处理

Cypher 已经内置了时间处理,数据库也支持存储时间作为节点和关系的属性

  • Date, Time, LocalTime, DateTime and LocalDateTime是时间类型,
  • Duration 不是时间类型

Temporal instants

  • 时间类型实例包括三个部分date, the time, and the timezone

  • 对于dates

  • 对于times

// Parsing a DateTime using the calendar date format:
RETURN datetime('2015-06-24T12:50:35.556+0100') AS theDateTime

// Parsing a LocalDateTime using the ordinal date format:
RETURN localdatetime('2015185T19:32:24') AS theLocalDateTime

//Parsing a Date using the week date format:
RETURN date('+2015-W13-4') AS theDate

//Parsing a Time:
RETURN time('125035.556+0100') AS theTime

// Parsing a LocalTime:
RETURN localtime('12:50:35.556') AS theLocalTime

//The following query shows how to extract the components of a DateTime value:
WITH date({ year:1984, month:10, day:11 }) AS d
RETURN d.year, d.quarter, d.month, d.week, d.weekYear, d.day, d.ordinalDay, d.dayOfWeek, d.dayOfQuarter

Durations

Durations代表时间量,捕获不同的时间类型实例之间的差值,可以为负数 使用P开头表示指定一个Durations类型,有两种方式构建Durations:

  • Unit-based form: P[nY][nM][nW][nD][T[nH][nM][nS]]
  • Date-and-time-based form: P<date>T<time>

//Return a Duration of 14 days, 16 hours and 12 minutes:
RETURN duration('P14DT16H12M') AS theDuration

// Return a Duration of 5 months, 1 day and 12 hours:
RETURN duration('P5M1.5D') AS theDuration

//Return a Duration of 45 seconds:
RETURN duration('PT0.75M') AS theDuration

//Return a Duration of 2 weeks, 3 days and 12 hours:
RETURN duration('P2.5W') AS theDuration

获取Duration成分:

如果要获取更小的成分:


WITH duration({ years: 1, months:4, days: 111, hours: 1, minutes: 1, seconds: 1, 
nanoseconds: 111111111 }) AS d
RETURN d.years, d.months, d.monthsOfYear, d.days, d.hours, 
d.minutes, d.minutesOfHour, d.seconds, d.secondsOfMinute, 
d.milliseconds, d.millisecondsOfSecond, d.microseconds, 
d.microsecondsOfSecond, d.nanoseconds, d.nanosecondsOfSecond


例子

//Create a Duration representing 1.5 days:
RETURN duration({ days: 1, hours: 12 }) AS theDuration

//Compute the Duration between two temporal instants:
RETURN duration.between(date('1984-10-11'), date('2015-06-24')) AS theDuration

//Compute the number of days between two Date values:
RETURN duration.inDays(date('2014-10-11'), date('2015-08-06')) AS theDuration

//Get the first Date of the current year:
RETURN date.truncate('year') AS day

//Get the Date of the Thursday in the week of a specific date:
RETURN date.truncate('week', date('2019-10-01'), { dayOfWeek: 4 }) AS thursday

//Get the Date of the last day of the next month:
RETURN date.truncate('month', date()+ duration('P2M'))- duration('P1D') AS lastDay

//Add a Duration to a Date:
RETURN time('13:42:19')+ duration({ days: 1, hours: 12 }) AS theTime

//Add two Duration values:
RETURN duration({ days: 2, hours: 7 })+ duration({ months: 1, hours: 18 }) AS theDuration

//Multiply a Duration by a number:
RETURN duration({ hours: 5, minutes: 21 })* 14 AS theDuration

//Examine whether two instants are less than one day apart:
WITH datetime('2015-07-21T21:40:32.142+0100') AS date1, datetime('2015-07-21T17:12:56.333+0100') AS date2
RETURN
CASE
WHEN date1 < date2
THEN date1 + duration("P1D")> date2
ELSE date2 + duration("P1D")> date1 END AS lessThanOneDayApart

Temporal indexing

  • All temporal types can be indexed

Lists

Lists in general 一般的List

RETURN [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] AS list
[0,1,2,3,4,5,6,7,8,9]

RETURN range(0, 10)[3]
3

List comprehension列表推导式

列表推导式:通过已经存在的list构建新的list


RETURN [x IN range(0,10) WHERE x % 2 = 0 | x^3] AS result

[0.0,8.0,64.0,216.0,512.0,1000.0]


Pattern comprehension模式推导式

模式推导式:通过模式匹配构建list

MATCH (a:Person { name: 'Keanu Reeves' })
RETURN [(a)-->(b) WHERE b:Movie | b.released] AS years

years
1 row
[1997,2003,2003,2000,1999,2003,1995]

Maps

Literal maps

Cypher支持构建map,Map的key必须是string类型

RETURN { key: 'Value', listKey: [{ inner: 'Map1' }, { inner: 'Map2' }]}

Map projection

Cypher支持Map 投影,可以很容易的从节点关系中构建map投影
map_variable {map_element, [, …n]} 不同的Map 投影元素

  • Property selector - Projects the property name as the key, and the value from the map_variable as the value for the projection.
  • Literal entry - This is a key-value pair, with the value being arbitrary expression key: <expression>.
  • Variable selector - Projects a variable, with the variable name as the key, and the value the variable is pointing to as the value of the projection. Its syntax is just the variable.
  • All-properties selector - projects all key-value pairs from the map_variable value.

  • Find ‘Charlie Sheen’ and return data about him and the movies he has acted in. This example shows an example of map projection with a literal entry, which in turn also uses map projection inside the aggregating collect().
MATCH (actor:Person { name: 'Charlie Sheen' })-[:ACTED_IN]->(movie:Movie)
RETURN actor { .name, .realName, movies: collect(movie { .title, .year })}

actor
1 row
{movies -> [{year -> 1979, title -> "Apocalypse Now"},{year -> 1984, title -> "Red Dawn"},{year -> 1987, title -> "Wall Street"}], realName -> "Carlos Irwin Estévez", name -> "Charlie Sheen"}
  • Find all persons that have acted in movies, and show number for each. This example introduces an variable with the count, and uses a variable selector to project the value.
MATCH (actor:Person)-[:ACTED_IN]->(movie:Movie)WITH actor, count(movie) AS nrOfMovies
RETURN actor { .name, nrOfMovies }

actor
2 rows
{nrOfMovies -> 2, name -> "Martin Sheen"}
{nrOfMovies -> 3, name -> "Charlie Sheen"}
  • Again, focusing on ‘Charlie Sheen’, this time returning all properties from the node. Here we use an all-properties selector to project all the node properties, and additionally, explicitly project the property age. Since this property does not exist on the node, a null value is projected instead.

MATCH (actor:Person { name: 'Charlie Sheen' })
RETURN actor { .*, .age } 

{realName -> "Carlos Irwin Estévez", name -> "Charlie Sheen", age -> <null>}

坐标值Spatial values

  • 每一个点可以有两维和三维64bit的float
  • 每一个点和坐标系相关
  • 坐标实例可以作为节点和关系的属性
  • 节点的坐标可以使用spatial index
  • distance函数可以用于坐标系

坐标系

Cypher提供四种坐标系

  • 地理坐标系Geographic coordinate reference systems
    • WGS-84: longitude, latitude (x, y)
    • WGS-84-3D: longitude, latitude, height (x, y, z)
  • 笛卡尔坐标系
    • Cartesian: x, y
    • Cartesian 3D: x, y, z

地理坐标系,可以使用longitude, latitude, height (x, y, z)指定,也可以使用x, y, z指定,但是使用x, y, z指定必须指定坐标系类型,否则被认为笛卡尔坐标系; longitude, latitude的单位是float的度,height的单位是米

WITH point({ latitude:toFloat('13.43'), longitude:toFloat('56.21')}) AS p1, point({ latitude:toFloat('13.10'), longitude:toFloat('56.41')}) AS p2
RETURN toInteger(distance(p1,p2)/1000) AS km

//二维和三维直接不能用distance,
WITH point({ x:3, y:0 }) AS p2d, point({ x:0, y:4, z:1 }) AS p3d
RETURN distance(p2d,p3d) AS bad, distance(p2d,point({ x:p3d.x, y:p3d.y })) AS good

坐标实例Spatial instants

Cypher可以推断point是属于哪个坐标系,

  • 维度,2d,3d
  • latitude and longitude是地理坐标,x,y就是笛卡尔坐标
RETURN 
point({ x:3, y:0 }) AS cartesian_2d, 
point({ x:0, y:4, z:1 }) AS cartesian_3d, 
point({ latitude: 12, longitude: 56 }) AS geo_2d, 
point({ latitude: 12, longitude: 56, height: 1000 }) AS geo_3d

  • 获取坐标实例的值
WITH point({ x:3, y:4 }) AS p
RETURN p.x, p.y, p.crs, p.srid


WITH point({ latitude:3, longitude:4, height: 4321 }) AS p
RETURN p.latitude, p.longitude, p.height, p.x, p.y, p.z, p.crs, p.srid

坐标索引Spatial index

对带有属性标签:Label(property)的节点进行index,这个节点将被索引为坐标索引spatial index; neo4j 使用B+树进行坐标索引

可比较性和可排序Comparability and Orderability

  • 不同坐标系之间是不可比较的
  • distance函数对于不同坐标系也是不能比较的
WITH point({ x:3, y:0 }) AS p2d, point({ x:0, y:4, z:1 }) AS p3d
RETURN distance(p2d,p3d), p2d < p3d, p2d = p3d, p2d <> p3d, distance(p2d,point({ x:p3d.x, y:p3d.y }))

  • 所有的类型都是可排序的
  • The Point types will be ordered after Numbers and before Temporal types
  • 不同坐标系使用SRID排序
UNWIND [point({ x:3, y:0 }), point({ x:0, y:4, z:1 }), point({ srid:4326, x:12, y:56 }), point({ srid:4979, x:12, y:56, z:1000 })] AS point
RETURN point
ORDER BY point

本文使用 文章同步助手 同步

相关文章

  • Nosql-neo4j-Cypher 语法(3)

    目录:[TOC] 时间日期处理 Cypher 已经内置了时间处理,数据库也支持存储时间作为节点和关系的属性 Dat...

  • Nosql-neo4j-Cypher 语法

    目录:[TOC] 值和类型 Values and types 属性类型 Property types 结构类型 S...

  • Nosql-neo4j-Cypher 语法(2)

    目录:[TOC] Cypher运算 Operators 运算概要 运算类型概要聚合操作DISTINCT属性运算.获...

  • Nosql-neo4j-Cypher 语句(3)

    目录:[TOC] RETURN RETURN 可以返回节点,关系及属性 WITH WITH语句允许查询链接在一起,...

  • MARKDOWN学习之路3

    MARKDOWN学习之路3 markdown介绍markdown语法标题语法列表语法区块引用语法字体语法分割线图片...

  • 语法3

    1. nobody后用第三人称单数 2. 虚拟语气 条件句可分为两类,一类为真实条件句,一类为虚拟语气。 真实If...

  • shell系列4-流程控制

    一. if 判断 语法1: 语法2: 语法3: 实操案例: 二. case语句 语法: 注意事项: case行尾必...

  • dart语法7-泛型

    dart语法1dart语法2-内置类型dart语法3-函数dart语法4-操作符dart语法5-异常dart语法6...

  • dart语法8-库

    dart语法1dart语法2-内置类型dart语法3-函数dart语法4-操作符dart语法5-异常dart语法6...

  • dart语法10-生成器

    dart语法1dart语法2-内置类型dart语法3-函数dart语法4-操作符dart语法5-异常dart语法6...

网友评论

      本文标题:Nosql-neo4j-Cypher 语法(3)

      本文链接:https://www.haomeiwen.com/subject/jtpanrtx.html