Neo4j数据类型 #
一、数据类型概述 #
Neo4j属性支持的数据类型分为以下几类:
text
数据类型:
├── 基本类型
│ ├── 数值类型
│ ├── 字符串
│ └── 布尔值
├── 集合类型
│ ├── 列表
│ └── 映射
├── 空间类型
│ ├── Point
│ └── 地理位置
├── 时间类型
│ ├── Date
│ ├── Time
│ ├── LocalTime
│ ├── DateTime
│ └── Duration
└── 特殊值
└── NULL
二、数值类型 #
2.1 整数类型 #
cypher
CREATE (n:Number {
small: 42,
large: 9223372036854775807,
negative: -100
})
整数范围:
| 类型 | 范围 |
|---|---|
| 64位整数 | -2^63 到 2^63-1 |
2.2 浮点类型 #
cypher
CREATE (n:Number {
pi: 3.14159,
scientific: 1.5e10,
negative: -0.5
})
2.3 数值运算 #
cypher
MATCH (n:Number)
RETURN
n.small + 10 AS add,
n.small - 10 AS subtract,
n.small * 2 AS multiply,
n.small / 2 AS divide,
n.small % 3 AS modulo
2.4 数值函数 #
cypher
RETURN
abs(-10) AS abs,
ceil(3.2) AS ceil,
floor(3.8) AS floor,
round(3.5) AS round,
sign(-5) AS sign,
rand() AS random
2.5 类型转换 #
cypher
RETURN
toInteger('42') AS int,
toInteger(3.9) AS int_floor,
toFloat('3.14') AS float
三、字符串类型 #
3.1 字符串定义 #
cypher
CREATE (n:Person {
name: 'Tom',
description: "It's a test",
multiline: 'Line1
Line2
Line3'
})
3.2 字符串操作 #
cypher
MATCH (n:Person)
RETURN
n.name + ' Hanks' AS full_name,
size(n.name) AS length,
toUpper(n.name) AS upper,
toLower(n.name) AS lower,
trim(' hello ') AS trimmed,
ltrim(' hello ') AS ltrimmed,
rtrim(' hello ') AS rtrimmed
3.3 字符串截取 #
cypher
RETURN
substring('Hello World', 0, 5) AS sub,
left('Hello', 3) AS left_str,
right('Hello', 3) AS right_str
3.4 字符串查找 #
cypher
RETURN
'Hello World' CONTAINS 'World' AS contains,
'Hello' STARTS WITH 'He' AS starts,
'Hello' ENDS WITH 'lo' AS ends,
'Hello World' =~ 'He.*ld' AS regex
3.5 字符串替换 #
cypher
RETURN
replace('Hello World', 'World', 'Neo4j') AS replaced,
split('a,b,c', ',') AS split_list
3.6 字符串拼接 #
cypher
RETURN
'Hello' + ' ' + 'World' AS concat,
apoc.text.join(['Hello', 'World'], ' ') AS joined
四、布尔类型 #
4.1 布尔值 #
cypher
CREATE (n:Person {
active: true,
verified: false
})
4.2 布尔运算 #
cypher
RETURN
true AND false AS and_result,
true OR false AS or_result,
NOT true AS not_result,
true XOR false AS xor_result
4.3 比较结果 #
cypher
RETURN
5 > 3 AS greater,
5 = 5 AS equal,
5 <> 3 AS not_equal
五、列表类型 #
5.1 列表定义 #
cypher
CREATE (n:Person {
tags: ['developer', 'designer', 'manager'],
scores: [85, 90, 78, 92],
mixed: [1, 'two', true, null]
})
5.2 列表访问 #
cypher
WITH [1, 2, 3, 4, 5] AS list
RETURN
list[0] AS first,
list[2] AS third,
list[-1] AS last,
list[1..3] AS range,
list[0..] AS from_start,
list[..3] AS to_third
5.3 列表函数 #
cypher
WITH [1, 2, 3, 4, 5] AS list
RETURN
size(list) AS size,
head(list) AS head,
last(list) AS last,
tail(list) AS tail
5.4 列表操作 #
cypher
RETURN
[1, 2] + [3, 4] AS concat,
[1, 2, 3, 2, 1] AS original,
range(1, 10) AS range_list,
range(1, 10, 2) AS step_range
5.5 列表推导 #
cypher
WITH [1, 2, 3, 4, 5] AS list
RETURN [x IN list WHERE x > 2 | x * 2] AS filtered_doubled
5.6 列表聚合 #
cypher
MATCH (n:Person)
RETURN collect(n.name) AS names
5.7 列表展开 #
cypher
WITH [1, 2, 3] AS list
UNWIND list AS item
RETURN item
六、映射类型 #
6.1 映射定义 #
cypher
CREATE (n:Person {
name: 'Tom',
address: {
city: 'New York',
country: 'USA',
zip: '10001'
}
})
6.2 映射访问 #
cypher
MATCH (n:Person)
RETURN
n.address.city AS city,
n.address['country'] AS country
6.3 映射函数 #
cypher
WITH {name: 'Tom', age: 30} AS map
RETURN
keys(map) AS keys,
values(map) AS values,
map.name AS name
6.4 映射合并 #
cypher
WITH {a: 1, b: 2} AS map1, {b: 3, c: 4} AS map2
RETURN map1 {.*}, map2 {.*} AS merged
七、空间类型 #
7.1 Point类型 #
cypher
CREATE (n:Location {
point: point({latitude: 40.7128, longitude: -74.0060}),
cartesian: point({x: 10, y: 20})
})
7.2 坐标系统 #
| CRS | 说明 | 使用场景 |
|---|---|---|
| WGS-84 | 地理坐标(经纬度) | 地球表面位置 |
| WGS-84-3D | 地理坐标3D | 海拔高度 |
| Cartesian | 笛卡尔坐标 | 平面坐标 |
| Cartesian-3D | 笛卡尔坐标3D | 三维空间 |
7.3 空间函数 #
cypher
WITH
point({latitude: 40.7128, longitude: -74.0060}) AS nyc,
point({latitude: 34.0522, longitude: -118.2437}) AS la
RETURN
point.distance(nyc, la) / 1000 AS distance_km
7.4 空间查询 #
cypher
MATCH (n:Location)
WHERE point.distance(n.point, point({latitude: 40.7, longitude: -74.0})) < 10000
RETURN n
八、时间类型 #
8.1 Date类型 #
cypher
CREATE (n:Event {
date: date('2024-01-15'),
date_today: date(),
date_components: date({year: 2024, month: 1, day: 15})
})
8.2 Time类型 #
cypher
CREATE (n:Event {
time: time('14:30:00'),
time_tz: time('14:30:00+08:00'),
time_now: time()
})
8.3 LocalTime类型 #
cypher
CREATE (n:Event {
local_time: localtime('14:30:00'),
local_time_now: localtime()
})
8.4 DateTime类型 #
cypher
CREATE (n:Event {
datetime: datetime('2024-01-15T14:30:00+08:00'),
datetime_now: datetime(),
datetime_components: datetime({
year: 2024,
month: 1,
day: 15,
hour: 14,
minute: 30,
second: 0,
timezone: '+08:00'
})
})
8.5 Duration类型 #
cypher
CREATE (n:Event {
duration: duration('P1Y2M3DT4H5M6S'),
duration_components: duration({years: 1, months: 2, days: 3})
})
8.6 时间运算 #
cypher
WITH date('2024-01-15') AS d
RETURN
d + duration('P1M') AS next_month,
d - duration('P1D') AS yesterday
8.7 时间函数 #
cypher
WITH datetime() AS dt
RETURN
dt.year AS year,
dt.month AS month,
dt.day AS day,
dt.hour AS hour,
dt.minute AS minute,
dt.second AS second,
dt.dayOfWeek AS day_of_week,
dt.week AS week
8.8 时间格式化 #
cypher
WITH datetime() AS dt
RETURN
apoc.temporal.format(dt, 'yyyy-MM-dd HH:mm:ss') AS formatted
九、NULL值 #
9.1 NULL定义 #
cypher
CREATE (n:Person {
name: 'Tom',
middle_name: null
})
9.2 NULL判断 #
cypher
MATCH (n:Person)
WHERE n.middle_name IS NULL
RETURN n
MATCH (n:Person)
WHERE n.middle_name IS NOT NULL
RETURN n
9.3 NULL处理 #
cypher
MATCH (n:Person)
RETURN
n.name,
n.middle_name,
COALESCE(n.middle_name, 'N/A') AS middle_name_or_default,
n.name + COALESCE(n.middle_name, '') AS full_name
9.4 NULL传播 #
cypher
RETURN
null + 1 AS result,
null * 2 AS result2,
null = null AS result3
十、类型判断 #
10.1 类型检查函数 #
cypher
RETURN
apoc.meta.type(42) AS int_type,
apoc.meta.type('hello') AS string_type,
apoc.meta.type([1, 2, 3]) AS list_type,
apoc.meta.type({a: 1}) AS map_type
10.2 类型判断 #
cypher
WITH 'hello' AS value
RETURN
apoc.meta.isType(value, 'STRING') AS is_string,
apoc.meta.isType(value, 'INTEGER') AS is_integer
十一、类型转换 #
11.1 基本转换 #
cypher
RETURN
toString(42) AS str,
toInteger('42') AS int,
toFloat('3.14') AS float,
toBoolean('true') AS bool
11.2 时间转换 #
cypher
RETURN
date('2024-01-15') AS date_val,
datetime('2024-01-15T14:30:00') AS datetime_val,
toString(date('2024-01-15')) AS date_str
11.3 JSON转换 #
cypher
WITH {name: 'Tom', age: 30} AS map
RETURN
apoc.convert.toJson(map) AS json_str,
apoc.convert.fromJsonMap('{"name":"Tom","age":30}') AS json_map
十二、节点和关系类型 #
12.1 节点类型 #
cypher
MATCH (n:Person)
RETURN
n AS node,
labels(n) AS labels,
keys(n) AS properties,
properties(n) AS property_map
12.2 关系类型 #
cypher
MATCH ()-[r:KNOWS]->()
RETURN
r AS relationship,
type(r) AS type,
keys(r) AS properties,
properties(r) AS property_map
12.3 路径类型 #
cypher
MATCH p = (a)-[:KNOWS*1..3]->(b)
RETURN
p AS path,
length(p) AS path_length,
nodes(p) AS path_nodes,
relationships(p) AS path_rels
十三、类型总结 #
13.1 属性支持类型 #
| 类型 | 示例 | 说明 |
|---|---|---|
| Integer | 42 | 64位整数 |
| Float | 3.14 | 64位浮点数 |
| String | ‘hello’ | Unicode字符串 |
| Boolean | true | 布尔值 |
| Point | point({x:1, y:2}) | 空间坐标 |
| Date | date(‘2024-01-15’) | 日期 |
| Time | time(‘14:30:00’) | 时间 |
| LocalTime | localtime(‘14:30:00’) | 本地时间 |
| DateTime | datetime(‘2024-01-15T14:30:00’) | 日期时间 |
| Duration | duration(‘P1D’) | 时间间隔 |
| List | [1, 2, 3] | 列表 |
| Map | 映射(仅参数) |
13.2 非属性类型 #
| 类型 | 说明 |
|---|---|
| Node | 节点引用 |
| Relationship | 关系引用 |
| Path | 路径引用 |
十四、最佳实践 #
14.1 类型选择建议 #
text
建议:
├── 使用合适的最小类型
├── 时间使用原生时间类型
├── 地理位置使用Point类型
├── 避免在属性中存储大型列表
└── 保持类型一致性
14.2 NULL处理建议 #
text
建议:
├── 明确处理NULL值
├── 使用COALESCE提供默认值
├── 使用IS NULL/IS NOT NULL判断
└── 避免NULL参与计算
下一步,让我们学习图数据库的核心概念:节点!
最后更新:2026-03-27