ArangoDB快速入门案例

此去经年,夏梦未央。我以为你会是我最温暖的夏阳。只是我忘了,我们都无力阻止四季的轮转。冬天过后,还是会有令人憎恨的闷热。是你浮夸了那一年寒冷的冬天。还是那一年的雪花蒸发了整个曾经

Posted by yishuifengxiao on 2021-12-27

一 准备数据

1.1 创建Characters数据表

先创建一个名为 Characters的数据表,然后插入以下数据:

LET data = [
{ "name": "Robert", "surname": "Baratheon", "alive": false, "traits": ["A","H","C"] },
{ "name": "Jaime", "surname": "Lannister", "alive": true, "age": 36, "traits": ["A","F","B"] },
{ "name": "Catelyn", "surname": "Stark", "alive": false, "age": 40, "traits": ["D","H","C"] },
{ "name": "Cersei", "surname": "Lannister", "alive": true, "age": 36, "traits": ["H","E","F"] },
{ "name": "Daenerys", "surname": "Targaryen", "alive": true, "age": 16, "traits": ["D","H","C"] },
{ "name": "Jorah", "surname": "Mormont", "alive": false, "traits": ["A","B","C","F"] },
{ "name": "Petyr", "surname": "Baelish", "alive": false, "traits": ["E","G","F"] },
{ "name": "Viserys", "surname": "Targaryen", "alive": false, "traits": ["O","L","N"] },
{ "name": "Jon", "surname": "Snow", "alive": true, "age": 16, "traits": ["A","B","C","F"] },
{ "name": "Sansa", "surname": "Stark", "alive": true, "age": 13, "traits": ["D","I","J"] },
{ "name": "Arya", "surname": "Stark", "alive": true, "age": 11, "traits": ["C","K","L"] },
{ "name": "Robb", "surname": "Stark", "alive": false, "traits": ["A","B","C","K"] },
{ "name": "Theon", "surname": "Greyjoy", "alive": true, "age": 16, "traits": ["E","R","K"] },
{ "name": "Bran", "surname": "Stark", "alive": true, "age": 10, "traits": ["L","J"] },
{ "name": "Joffrey", "surname": "Baratheon", "alive": false, "age": 19, "traits": ["I","L","O"] },
{ "name": "Sandor", "surname": "Clegane", "alive": true, "traits": ["A","P","K","F"] },
{ "name": "Tyrion", "surname": "Lannister", "alive": true, "age": 32, "traits": ["F","K","M","N"] },
{ "name": "Khal", "surname": "Drogo", "alive": false, "traits": ["A","C","O","P"] },
{ "name": "Tywin", "surname": "Lannister", "alive": false, "traits": ["O","M","H","F"] },
{ "name": "Davos", "surname": "Seaworth", "alive": true, "age": 49, "traits": ["C","K","P","F"] },
{ "name": "Samwell", "surname": "Tarly", "alive": true, "age": 17, "traits": ["C","L","I"] },
{ "name": "Stannis", "surname": "Baratheon", "alive": false, "traits": ["H","O","P","M"] },
{ "name": "Melisandre", "alive": true, "traits": ["G","E","H"] },
{ "name": "Margaery", "surname": "Tyrell", "alive": false, "traits": ["M","D","B"] },
{ "name": "Jeor", "surname": "Mormont", "alive": false, "traits": ["C","H","M","P"] },
{ "name": "Bronn", "alive": true, "traits": ["K","E","C"] },
{ "name": "Varys", "alive": true, "traits": ["M","F","N","E"] },
{ "name": "Shae", "alive": false, "traits": ["M","D","G"] },
{ "name": "Talisa", "surname": "Maegyr", "alive": false, "traits": ["D","C","B"] },
{ "name": "Gendry", "alive": false, "traits": ["K","C","A"] },
{ "name": "Ygritte", "alive": false, "traits": ["A","P","K"] },
{ "name": "Tormund", "surname": "Giantsbane", "alive": true, "traits": ["C","P","A","I"] },
{ "name": "Gilly", "alive": true, "traits": ["L","J"] },
{ "name": "Brienne", "surname": "Tarth", "alive": true, "age": 32, "traits": ["P","C","A","K"] },
{ "name": "Ramsay", "surname": "Bolton", "alive": true, "traits": ["E","O","G","A"] },
{ "name": "Ellaria", "surname": "Sand", "alive": true, "traits": ["P","O","A","E"] },
{ "name": "Daario", "surname": "Naharis", "alive": true, "traits": ["K","P","A"] },
{ "name": "Missandei", "alive": true, "traits": ["D","L","C","M"] },
{ "name": "Tommen", "surname": "Baratheon", "alive": true, "traits": ["I","L","B"] },
{ "name": "Jaqen", "surname": "H'ghar", "alive": true, "traits": ["H","F","K"] },
{ "name": "Roose", "surname": "Bolton", "alive": true, "traits": ["H","E","F","A"] },
{ "name": "The High Sparrow", "alive": true, "traits": ["H","M","F","O"] }
]

FOR d IN data
INSERT d INTO Characters

执行完上述命令后,再执行以下命令,查看是否执行成功

FOR c IN Characters
limit 2
RETURN c

得到如下执行结果:

[
{
"_key": "109566",
"_id": "Characters/109566",
"_rev": "_ddlhG6C--_",
"name": "Robert",
"surname": "Baratheon",
"alive": false,
"traits": [
"A",
"H",
"C"
]
},
{
"_key": "109567",
"_id": "Characters/109567",
"_rev": "_ddlhG6C--B",
"name": "Jaime",
"surname": "Lannister",
"alive": true,
"age": 36,
"traits": [
"A",
"F",
"B"
]
}
]

1.2 创建Traits 数据表

1.2.1 创建并插入数据

创建一个名为Traits的数据表,然后执行以下命令:

LET data =[
{ "_key": "A", "en": "strong", "de": "stark" },
{ "_key": "B", "en": "polite", "de": "freundlich" },
{ "_key": "C", "en": "loyal", "de": "loyal" },
{ "_key": "D", "en": "beautiful", "de": "schön" },
{ "_key": "E", "en": "sneaky", "de": "hinterlistig" },
{ "_key": "F", "en": "experienced", "de": "erfahren" },
{ "_key": "G", "en": "corrupt", "de": "korrupt" },
{ "_key": "H", "en": "powerful", "de": "einflussreich" },
{ "_key": "I", "en": "naive", "de": "naiv" },
{ "_key": "J", "en": "unmarried", "de": "unverheiratet" },
{ "_key": "K", "en": "skillful", "de": "geschickt" },
{ "_key": "L", "en": "young", "de": "jung" },
{ "_key": "M", "en": "smart", "de": "klug" },
{ "_key": "N", "en": "rational", "de": "rational" },
{ "_key": "O", "en": "ruthless", "de": "skrupellos" },
{ "_key": "P", "en": "brave", "de": "mutig" },
{ "_key": "Q", "en": "mighty", "de": "mächtig" },
{ "_key": "R", "en": "weak", "de": "schwach" }
]

for d in data
INSERT d INTO Traits

然后执行下面命令,查看数据是否插入成功

FOR c IN Traits
limit 2
RETURN c

得到的运行结果如下

[
{
"_key": "A",
"_id": "Traits/A",
"_rev": "_ddllNrK--_",
"en": "strong",
"de": "stark"
},
{
"_key": "B",
"_id": "Traits/B",
"_rev": "_ddllNrK--B",
"en": "polite",
"de": "freundlich"
}
]

1.2.2 跨表查询

结合上面两个数据库查询

执行以下命令

FOR c IN Characters
limit 2
RETURN DOCUMENT("Traits", c.traits)

得到的运行结果如下:

[
[
{
"_key": "A",
"_id": "Traits/A",
"_rev": "_ddllNrK--_",
"en": "strong",
"de": "stark"
},
{
"_key": "H",
"_id": "Traits/H",
"_rev": "_ddllNrK--N",
"en": "powerful",
"de": "einflussreich"
},
{
"_key": "C",
"_id": "Traits/C",
"_rev": "_ddllNrK--D",
"en": "loyal",
"de": "loyal"
}
],
[
{
"_key": "A",
"_id": "Traits/A",
"_rev": "_ddllNrK--_",
"en": "strong",
"de": "stark"
},
{
"_key": "F",
"_id": "Traits/F",
"_rev": "_ddllNrK--J",
"en": "experienced",
"de": "erfahren"
},
{
"_key": "B",
"_id": "Traits/B",
"_rev": "_ddllNrK--B",
"en": "polite",
"de": "freundlich"
}
]
]

注意: 返回结果为两个数组

image-20211227112512762

1.2.3 合并查询结果

执行以下命令

FOR c IN Characters
limit 2
RETURN MERGE(c, { traits: DOCUMENT("Traits", c.traits)[*].en } )

得到的运行结果如下

[
{
"_id": "Characters/109566",
"_key": "109566",
"_rev": "_ddlhG6C--_",
"alive": false,
"name": "Robert",
"surname": "Baratheon",
"traits": [
"strong",
"powerful",
"loyal"
]
},
{
"_id": "Characters/109567",
"_key": "109567",
"_rev": "_ddlhG6C--B",
"age": 36,
"alive": true,
"name": "Jaime",
"surname": "Lannister",
"traits": [
"strong",
"experienced",
"polite"
]
}
]

1.3 创建ChildOf 边缘表

创建一个名为ChildOf的边缘集合。

然后插入数据

LET data = [
{
"parent": { "name": "Ned", "surname": "Stark" },
"child": { "name": "Robb", "surname": "Stark" }
}, {
"parent": { "name": "Ned", "surname": "Stark" },
"child": { "name": "Sansa", "surname": "Stark" }
}, {
"parent": { "name": "Ned", "surname": "Stark" },
"child": { "name": "Arya", "surname": "Stark" }
}, {
"parent": { "name": "Ned", "surname": "Stark" },
"child": { "name": "Bran", "surname": "Stark" }
}, {
"parent": { "name": "Catelyn", "surname": "Stark" },
"child": { "name": "Robb", "surname": "Stark" }
}, {
"parent": { "name": "Catelyn", "surname": "Stark" },
"child": { "name": "Sansa", "surname": "Stark" }
}, {
"parent": { "name": "Catelyn", "surname": "Stark" },
"child": { "name": "Arya", "surname": "Stark" }
}, {
"parent": { "name": "Catelyn", "surname": "Stark" },
"child": { "name": "Bran", "surname": "Stark" }
}, {
"parent": { "name": "Ned", "surname": "Stark" },
"child": { "name": "Jon", "surname": "Snow" }
}, {
"parent": { "name": "Tywin", "surname": "Lannister" },
"child": { "name": "Jaime", "surname": "Lannister" }
}, {
"parent": { "name": "Tywin", "surname": "Lannister" },
"child": { "name": "Cersei", "surname": "Lannister" }
}, {
"parent": { "name": "Tywin", "surname": "Lannister" },
"child": { "name": "Tyrion", "surname": "Lannister" }
}, {
"parent": { "name": "Cersei", "surname": "Lannister" },
"child": { "name": "Joffrey", "surname": "Baratheon" }
}, {
"parent": { "name": "Jaime", "surname": "Lannister" },
"child": { "name": "Joffrey", "surname": "Baratheon" }
}
]

FOR rel in data
LET parentId = FIRST(
FOR c IN Characters
FILTER c.name == rel.parent.name
FILTER c.surname == rel.parent.surname
LIMIT 1
RETURN c._id
)
LET childId = FIRST(
FOR c IN Characters
FILTER c.name == rel.child.name
FILTER c.surname == rel.child.surname
LIMIT 1
RETURN c._id
)
FILTER parentId != null AND childId != null
INSERT { _from: childId, _to: parentId } INTO ChildOf
RETURN NEW

执行完成上述命令后,得到的响应如下

image-20211227113419643

将其中的两个结果放大

image-20211227113546372

image-20211227113625844

也可以使用以下命令直接插入数据

INSERT { _from: "Characters/robb", _to: "Characters/ned" } INTO ChildOf

其中的Characters/robbCharacters/ned分别为两个数据的id。

二 遍历数据

2.1 查询父节点(OUTBOUND查询_to)

2.1.1 查询示例

2.1.1.1 示例1

使用下面的查询语句进行查询

FOR v IN 1..1 OUTBOUND "Characters/109568" ChildOf
RETURN v

得到的查询结果如下

[]

即查询出来的数据为空。


2.1.1.2 示例2

使用下面的查询语句进行查询

FOR v IN 1..1 OUTBOUND "Characters/109576" ChildOf
RETURN v

得到的查询结果如下

[
{
"_key": "109568",
"_id": "Characters/109568",
"_rev": "_ddlhG6C--D",
"name": "Catelyn",
"surname": "Stark",
"alive": false,
"age": 40,
"traits": [
"D",
"H",
"C"
]
}
]

2.1.1.3 示例3

使用下面的语句进行查询

FOR v IN 1..1 OUTBOUND "Characters/109569" ChildOf
RETURN v

得到的结果如下

[
{
"_key": "109584",
"_id": "Characters/109584",
"_rev": "_ddlhG6G--D",
"name": "Tywin",
"surname": "Lannister",
"alive": false,
"traits": [
"O",
"M",
"H",
"F"
]
}
]

2.1.1.4 示例4

使用下面的查询语句进行查询

FOR v IN 1..2 OUTBOUND "Characters/109580" ChildOf
RETURN v

得到的结果如下

[
{
"_key": "109569",
"_id": "Characters/109569",
"_rev": "_ddlhG6C--F",
"name": "Cersei",
"surname": "Lannister",
"alive": true,
"age": 36,
"traits": [
"H",
"E",
"F"
]
},
{
"_key": "109584",
"_id": "Characters/109584",
"_rev": "_ddlhG6G--D",
"name": "Tywin",
"surname": "Lannister",
"alive": false,
"traits": [
"O",
"M",
"H",
"F"
]
},
{
"_key": "109567",
"_id": "Characters/109567",
"_rev": "_ddlhG6C--B",
"name": "Jaime",
"surname": "Lannister",
"alive": true,
"age": 36,
"traits": [
"A",
"F",
"B"
]
},
{
"_key": "109584",
"_id": "Characters/109584",
"_rev": "_ddlhG6G--D",
"name": "Tywin",
"surname": "Lannister",
"alive": false,
"traits": [
"O",
"M",
"H",
"F"
]
}
]

一共返回四条数据(其中109584这个节点重复出现一次


2.1.1.5 示例5

使用下面的命令进行查询

FOR v IN 1..1 OUTBOUND  ChildOf "Characters/109580"
RETURN v

此查询语句会产生错误

image-20211227132836003

2.1.2 结论

使用图表方式展示边缘集合 ChildOf 的数据如下

image-20211227123728491

从上述查询语句和结果可以看出:

  • OUTBOUND关键字用于查询当前节点所指向的_to节点。
  • INOUTBOUND关键字之间的1..2决定着查找深度。
  • 查询结果默认不会去重,在多个遍历路径时若有相同的节点参与遍历,该节点可能会出现多次。

2.2 查询子节点(INBOUND查询_from)

2.2.1 查询示例

2.2.1.1 示例1

使用下面的语句进行查询

FOR v IN 1..1 INBOUND "Characters/109568" ChildOf
RETURN v

得到的结果如下

[
{
"_key": "109577",
"_id": "Characters/109577",
"_rev": "_ddlhG6C--V",
"name": "Robb",
"surname": "Stark",
"alive": false,
"traits": [
"A",
"B",
"C",
"K"
]
},
{
"_key": "109575",
"_id": "Characters/109575",
"_rev": "_ddlhG6C--R",
"name": "Sansa",
"surname": "Stark",
"alive": true,
"age": 13,
"traits": [
"D",
"I",
"J"
]
},
{
"_key": "109576",
"_id": "Characters/109576",
"_rev": "_ddlhG6C--T",
"name": "Arya",
"surname": "Stark",
"alive": true,
"age": 11,
"traits": [
"C",
"K",
"L"
]
},
{
"_key": "109579",
"_id": "Characters/109579",
"_rev": "_ddlhG6C--Z",
"name": "Bran",
"surname": "Stark",
"alive": true,
"age": 10,
"traits": [
"L",
"J"
]
}
]

2.2.1.1 示例2

使用下面的语句进行查询

FOR v IN 1..1 InBOUND "Characters/109576" ChildOf
RETURN v

得到的查询结果如下

[]

即查询出来的数据为空。


2.2.1.3 示例3

使用下面的语句进行查询

FOR v IN 1..1 INBOUND "Characters/109569" ChildOf
RETURN v

得到的结果如下

[
{
"_key": "109580",
"_id": "Characters/109580",
"_rev": "_ddlhG6C--b",
"name": "Joffrey",
"surname": "Baratheon",
"alive": false,
"age": 19,
"traits": [
"I",
"L",
"O"
]
}
]

2.2.1.4 示例4

使用下面的查询语句进行查询

FOR v IN 1..2 INBOUND "Characters/109580" ChildOf
RETURN v

得到的结果如下

[
{
"_key": "109567",
"_id": "Characters/109567",
"_rev": "_ddlhG6C--B",
"name": "Jaime",
"surname": "Lannister",
"alive": true,
"age": 36,
"traits": [
"A",
"F",
"B"
]
},
{
"_key": "109580",
"_id": "Characters/109580",
"_rev": "_ddlhG6C--b",
"name": "Joffrey",
"surname": "Baratheon",
"alive": false,
"age": 19,
"traits": [
"I",
"L",
"O"
]
},
{
"_key": "109569",
"_id": "Characters/109569",
"_rev": "_ddlhG6C--F",
"name": "Cersei",
"surname": "Lannister",
"alive": true,
"age": 36,
"traits": [
"H",
"E",
"F"
]
},
{
"_key": "109580",
"_id": "Characters/109580",
"_rev": "_ddlhG6C--b",
"name": "Joffrey",
"surname": "Baratheon",
"alive": false,
"age": 19,
"traits": [
"I",
"L",
"O"
]
},
{
"_key": "109582",
"_id": "Characters/109582",
"_rev": "_ddlhG6G--_",
"name": "Tyrion",
"surname": "Lannister",
"alive": true,
"age": 32,
"traits": [
"F",
"K",
"M",
"N"
]
}
]

使用表格展示如下

image-20211227133628561


2.2.1.5 示例5

使用下面的命令进行查询

FOR v IN 1..1 INBOUND  ChildOf "Characters/109569"
RETURN v

此查询语句会产生错误

image-20211227134031770

2.2.2 结论

根据上面的查询示例语句及其结果可以得知:

  • INBOUND关键字用于查询当前节点所指向的_from节点
  • ININBOUND关键字之间的1..2决定着查找深度。

2.3 查询孙节点(INBOUND查询_from)

2.3.1 基本示例

使用查询的查询语句进行查询

FOR c IN Characters
FILTER c.name == "Tywin"
FOR v IN 2..2 INBOUND c ChildOf
RETURN v

注意这里的起点为name为Tywin的点。(即起点为Characters/109584 的节点)

得到的查询结果如下

image-20211227135014304

使用JSON数据表示为

[
{
"_key": "109580",
"_id": "Characters/109580",
"_rev": "_ddlhG6C--b",
"name": "Joffrey",
"surname": "Baratheon",
"alive": false,
"age": 19,
"traits": [
"I",
"L",
"O"
]
},
{
"_key": "109580",
"_id": "Characters/109580",
"_rev": "_ddlhG6C--b",
"name": "Joffrey",
"surname": "Baratheon",
"alive": false,
"age": 19,
"traits": [
"I",
"L",
"O"
]
}
]

注意,在上述查询结果中, 得到的数据默认不会进行去重处理

2.3.2 数据去重

在上面所有的查询语句中,得到查询结果默认都不会进行去重处理,为了对结果进行去重,可以使用 DISTINCT关键字

使用下面的语句查询:

FOR c IN Characters
FILTER c.name == "Tywin"
FOR v IN 2..2 INBOUND c ChildOf
RETURN DISTINCT (v)

得到的结果如下

image-20211227135727826

JSON数据表示为

[
{
"_key": "109580",
"_id": "Characters/109580",
"_rev": "_ddlhG6C--b",
"name": "Joffrey",
"surname": "Baratheon",
"alive": false,
"age": 19,
"traits": [
"I",
"L",
"O"
]
}
]

三 聚合查询

3.1 查询数量统计

结合上面的查询数据,使用下面的语句进行查询

FOR c IN Characters
FILTER c.name == "Tywin"
FOR v IN 2..2 INBOUND c ChildOf
COLLECT actor = v WITH COUNT INTO counter
RETURN counter

得到的查询结果如下

[
2
]

可以看到,在使用上述查询语句时,数据库未对相同的节点进行去重统计


若使用系统中提供的 lengthCOUNT关键字进行查询,则得到的结构如下

注意:在arangodb中COUNTlength的别名

FOR c IN Characters
FILTER c.name == "Tywin"
FOR v IN 2..2 INBOUND c ChildOf
return COUNT(v)

得到的结果如下

[
8,
8
]

这里统计的结果里元素的属性的数量。

四 使用示例

4.1 数据准备

4.1.1 准备actors数据

先创建一个名为 actors的数据表,然后准备一下数据

LET data=[
{
"_key": "Keanu",
"_id": "actors/Keanu",
"_rev": "_ddo5sIW---",
"name": "Keanu Reeves",
"born": 1964
},
{
"_key": "Carrie",
"_id": "actors/Carrie",
"_rev": "_ddo5sLO---",
"name": "Carrie-Anne Moss",
"born": 1967
},
{
"_key": "Laurence",
"_id": "actors/Laurence",
"_rev": "_ddo5sii---",
"name": "Laurence Fishburne",
"born": 1961
},
{
"_key": "Hugo",
"_id": "actors/Hugo",
"_rev": "_ddo5sk2---",
"name": "Hugo Weaving",
"born": 1960
},
{
"_key": "Emil",
"_id": "actors/Emil",
"_rev": "_ddo5soC---",
"name": "Emil Eifrem",
"born": 1978
},
{
"_key": "Charlize",
"_id": "actors/Charlize",
"_rev": "_ddo5ttS---",
"name": "Charlize Theron",
"born": 1975
},
{
"_key": "Al",
"_id": "actors/Al",
"_rev": "_ddo5tw6---",
"name": "Al Pacino",
"born": 1940
},
{
"_key": "TomC",
"_id": "actors/TomC",
"_rev": "_ddo5uNe---",
"name": "Tom Cruise",
"born": 1962
},
{
"_key": "JackN",
"_id": "actors/JackN",
"_rev": "_ddo5uQC---",
"name": "Jack Nicholson",
"born": 1937
},
{
"_key": "DemiM",
"_id": "actors/DemiM",
"_rev": "_ddo5uTS---",
"name": "Demi Moore",
"born": 1962
},
{
"_key": "KevinB",
"_id": "actors/KevinB",
"_rev": "_ddo5uV2---",
"name": "Kevin Bacon",
"born": 1958
},
{
"_key": "KieferS",
"_id": "actors/KieferS",
"_rev": "_ddo5uaK---",
"name": "Kiefer Sutherland",
"born": 1966
},
{
"_key": "NoahW",
"_id": "actors/NoahW",
"_rev": "_ddo5ucy---",
"name": "Noah Wyle",
"born": 1971
},
{
"_key": "CubaG",
"_id": "actors/CubaG",
"_rev": "_ddo5ufW---",
"name": "Cuba Gooding Jr.",
"born": 1968
},
{
"_key": "KevinP",
"_id": "actors/KevinP",
"_rev": "_ddo5uhS---",
"name": "Kevin Pollak",
"born": 1957
},
{
"_key": "JTW",
"_id": "actors/JTW",
"_rev": "_ddo5ujS---",
"name": "J.T. Walsh",
"born": 1943
},
{
"_key": "JamesM",
"_id": "actors/JamesM",
"_rev": "_ddo5umG---",
"name": "James Marshall",
"born": 1967
},
{
"_key": "ChristopherG",
"_id": "actors/ChristopherG",
"_rev": "_ddo5upy---",
"name": "Christopher Guest",
"born": 1948
},
{
"_key": "KellyM",
"_id": "actors/KellyM",
"_rev": "_ddo5vWu---",
"name": "Kelly McGillis",
"born": 1957
},
{
"_key": "ValK",
"_id": "actors/ValK",
"_rev": "_ddo5vZS---",
"name": "Val Kilmer",
"born": 1959
},
{
"_key": "AnthonyE",
"_id": "actors/AnthonyE",
"_rev": "_ddo5vb2---",
"name": "Anthony Edwards",
"born": 1962
},
{
"_key": "TomS",
"_id": "actors/TomS",
"_rev": "_ddo5veW---",
"name": "Tom Skerritt",
"born": 1933
},
{
"_key": "MegR",
"_id": "actors/MegR",
"_rev": "_ddo5vhi---",
"name": "Meg Ryan",
"born": 1961
},
{
"_key": "ReneeZ",
"_id": "actors/ReneeZ",
"_rev": "_ddo5v6m---",
"name": "Renee Zellweger",
"born": 1969
},
{
"_key": "KellyP",
"_id": "actors/KellyP",
"_rev": "_ddo5v9S---",
"name": "Kelly Preston",
"born": 1962
},
{
"_key": "JerryO",
"_id": "actors/JerryO",
"_rev": "_ddo5wAG---",
"name": "Jerry O'Connell",
"born": 1974
},
{
"_key": "JayM",
"_id": "actors/JayM",
"_rev": "_ddo5wCu---",
"name": "Jay Mohr",
"born": 1970
},
{
"_key": "BonnieH",
"_id": "actors/BonnieH",
"_rev": "_ddo5wHi---",
"name": "Bonnie Hunt",
"born": 1961
},
{
"_key": "ReginaK",
"_id": "actors/ReginaK",
"_rev": "_ddo5wK2---",
"name": "Regina King",
"born": 1971
},
{
"_key": "JonathanL",
"_id": "actors/JonathanL",
"_rev": "_ddo5wN2---",
"name": "Jonathan Lipnicki",
"born": 1996
},
{
"_key": "RiverP",
"_id": "actors/RiverP",
"_rev": "_ddo5w7i---",
"name": "River Phoenix",
"born": 1970
},
{
"_key": "CoreyF",
"_id": "actors/CoreyF",
"_rev": "_ddo5x-W---",
"name": "Corey Feldman",
"born": 1971
},
{
"_key": "WilW",
"_id": "actors/WilW",
"_rev": "_ddo5xB----",
"name": "Wil Wheaton",
"born": 1972
},
{
"_key": "JohnC",
"_id": "actors/JohnC",
"_rev": "_ddo5xDy---",
"name": "John Cusack",
"born": 1966
},
{
"_key": "MarshallB",
"_id": "actors/MarshallB",
"_rev": "_ddo5xGi---",
"name": "Marshall Bell",
"born": 1942
},
{
"_key": "HelenH",
"_id": "actors/HelenH",
"_rev": "_ddo5xoi---",
"name": "Helen Hunt",
"born": 1963
},
{
"_key": "GregK",
"_id": "actors/GregK",
"_rev": "_ddo5xrW---",
"name": "Greg Kinnear",
"born": 1963
},
{
"_key": "AnnabellaS",
"_id": "actors/AnnabellaS",
"_rev": "_ddo5yGy---",
"name": "Annabella Sciorra",
"born": 1960
},
{
"_key": "MaxS",
"_id": "actors/MaxS",
"_rev": "_ddo5yKm---",
"name": "Max von Sydow",
"born": 1929
},
{
"_key": "WernerH",
"_id": "actors/WernerH",
"_rev": "_ddo5yN6---",
"name": "Werner Herzog",
"born": 1942
},
{
"_key": "Robin",
"_id": "actors/Robin",
"_rev": "_ddo5yQi---",
"name": "Robin Williams",
"born": 1951
},
{
"_key": "EthanH",
"_id": "actors/EthanH",
"_rev": "_ddo5ypq---",
"name": "Ethan Hawke",
"born": 1970
},
{
"_key": "RickY",
"_id": "actors/RickY",
"_rev": "_ddo5ys----",
"name": "Rick Yune",
"born": 1971
},
{
"_key": "JamesC",
"_id": "actors/JamesC",
"_rev": "_ddo5yum---",
"name": "James Cromwell",
"born": 1940
},
{
"_key": "ParkerP",
"_id": "actors/ParkerP",
"_rev": "_ddo5zHK---",
"name": "Parker Posey",
"born": 1968
},
{
"_key": "DaveC",
"_id": "actors/DaveC",
"_rev": "_ddo5zKW---",
"name": "Dave Chappelle",
"born": 1973
},
{
"_key": "SteveZ",
"_id": "actors/SteveZ",
"_rev": "_ddo5zMy---",
"name": "Steve Zahn",
"born": 1967
},
{
"_key": "TomH",
"_id": "actors/TomH",
"_rev": "_ddo5zO6---",
"name": "Tom Hanks",
"born": 1956
},
{
"_key": "RitaW",
"_id": "actors/RitaW",
"_rev": "_ddo5zwW---",
"name": "Rita Wilson",
"born": 1956
},
{
"_key": "BillPull",
"_id": "actors/BillPull",
"_rev": "_ddo5zyu---",
"name": "Bill Pullman",
"born": 1953
},
{
"_key": "VictorG",
"_id": "actors/VictorG",
"_rev": "_ddo5z1a---",
"name": "Victor Garber",
"born": 1949
},
{
"_key": "RosieO",
"_id": "actors/RosieO",
"_rev": "_ddo5z5q---",
"name": "Rosie O'Donnell",
"born": 1962
},
{
"_key": "Nathan",
"_id": "actors/Nathan",
"_rev": "_ddo50V----",
"name": "Nathan Lane",
"born": 1956
},
{
"_key": "BillyC",
"_id": "actors/BillyC",
"_rev": "_ddo50qW---",
"name": "Billy Crystal",
"born": 1948
},
{
"_key": "CarrieF",
"_id": "actors/CarrieF",
"_rev": "_ddo50s2---",
"name": "Carrie Fisher",
"born": 1956
},
{
"_key": "BrunoK",
"_id": "actors/BrunoK",
"_rev": "_ddo50wm---",
"name": "Bruno Kirby",
"born": 1949
}
]

for d in data
INSERT d INTO actors

4.1.2 准备movies数据

创建一个名为 movies 的数据表,然后执行

LET data=[
{
"_key": "TheMatrix",
"_id": "movies/TheMatrix",
"_rev": "_ddo5sFG---",
"title": "The Matrix",
"released": 1999,
"tagline": "Welcome to the Real World"
},
{
"_key": "TheMatrixReloaded",
"_id": "movies/TheMatrixReloaded",
"_rev": "_ddo5t-----",
"title": "The Matrix Reloaded",
"released": 2003,
"tagline": "Free your mind"
},
{
"_key": "TheMatrixRevolutions",
"_id": "movies/TheMatrixRevolutions",
"_rev": "_ddo5tTW---",
"title": "The Matrix Revolutions",
"released": 2003,
"tagline": "Everything that has a beginning has an end"
},
{
"_key": "TheDevilsAdvocate",
"_id": "movies/TheDevilsAdvocate",
"_rev": "_ddo5tqi---",
"title": "The Devil's Advocate",
"released": 1997,
"tagline": "Evil has its winning ways"
},
{
"_key": "AFewGoodMen",
"_id": "movies/AFewGoodMen",
"_rev": "_ddo5uKq---",
"title": "A Few Good Men",
"released": 1992,
"tagline": "In the heart of the nation's capital, in a courthouse of the U.S. government, one man will stop at nothing to keep his honor, and one will stop at nothing to find the truth."
},
{
"_key": "TopGun",
"_id": "movies/TopGun",
"_rev": "_ddo5vUG---",
"title": "Top Gun",
"released": 1986,
"tagline": "I feel the need, the need for speed."
},
{
"_key": "JerryMaguire",
"_id": "movies/JerryMaguire",
"_rev": "_ddo5v4K---",
"title": "Jerry Maguire",
"released": 2000,
"tagline": "The rest of his life begins now."
},
{
"_key": "StandByMe",
"_id": "movies/StandByMe",
"_rev": "_ddo5w3e---",
"title": "Stand By Me",
"released": 1986,
"tagline": "For some, it's the last real taste of innocence, and the first real taste of life. But for everyone, it's the time that memories are made of."
},
{
"_key": "AsGoodAsItGets",
"_id": "movies/AsGoodAsItGets",
"_rev": "_ddo5xl2---",
"title": "As Good as It Gets",
"released": 1997,
"tagline": "A comedy from the heart that goes for the throat."
},
{
"_key": "WhatDreamsMayCome",
"_id": "movies/WhatDreamsMayCome",
"_rev": "_ddo5yDC---",
"title": "What Dreams May Come",
"released": 1998,
"tagline": "After life there is more. The end is just the beginning."
},
{
"_key": "SnowFallingonCedars",
"_id": "movies/SnowFallingonCedars",
"_rev": "_ddo5ymi---",
"title": "Snow Falling on Cedars",
"released": 1999,
"tagline": "First loves last. Forever."
},
{
"_key": "YouveGotMail",
"_id": "movies/YouveGotMail",
"_rev": "_ddo5zD2---",
"title": "You've Got Mail",
"released": 1998,
"tagline": "At odds in life... in love on-line."
},
{
"_key": "SleeplessInSeattle",
"_id": "movies/SleeplessInSeattle",
"_rev": "_ddo5zuG---",
"title": "Sleepless in Seattle",
"released": 1993,
"tagline": "What if someone you never met, someone you never saw, someone you never knew was the only someone for you?"
},
{
"_key": "JoeVersustheVolcano",
"_id": "movies/JoeVersustheVolcano",
"_rev": "_ddo50S2---",
"title": "Joe Versus the Volcano",
"released": 1990,
"tagline": "A story of love, lava and burning desire."
},
{
"_key": "WhenHarryMetSally",
"_id": "movies/WhenHarryMetSally",
"_rev": "_ddo50nm---",
"title": "When Harry Met Sally",
"released": 1998,
"tagline": "At odds in life... in love on-line."
}
]

for d in data
INSERT d INTO movies

4.1.3 准备actsIn数据

创建一个名为 actsIn 的边缘集合,然后执行

let data=[
{
"_key": "172",
"_id": "actsIn/172",
"_from": "actors/Keanu",
"_to": "movies/TheMatrix",
"_rev": "_ddo5sqm---",
"roles": [
"Neo"
],
"year": 1999
},
{
"_key": "174",
"_id": "actsIn/174",
"_from": "actors/Carrie",
"_to": "movies/TheMatrix",
"_rev": "_ddo5ss6---",
"roles": [
"Trinity"
],
"year": 1999
},
{
"_key": "176",
"_id": "actsIn/176",
"_from": "actors/Laurence",
"_to": "movies/TheMatrix",
"_rev": "_ddo5svG---",
"roles": [
"Morpheus"
],
"year": 1999
},
{
"_key": "178",
"_id": "actsIn/178",
"_from": "actors/Hugo",
"_to": "movies/TheMatrix",
"_rev": "_ddo5sxS---",
"roles": [
"Agent Smith"
],
"year": 1999
},
{
"_key": "180",
"_id": "actsIn/180",
"_from": "actors/Emil",
"_to": "movies/TheMatrix",
"_rev": "_ddo5szW---",
"roles": [
"Emil"
],
"year": 1999
},
{
"_key": "183",
"_id": "actsIn/183",
"_from": "actors/Keanu",
"_to": "movies/TheMatrixReloaded",
"_rev": "_ddo5tBa---",
"roles": [
"Neo"
],
"year": 2003
},
{
"_key": "185",
"_id": "actsIn/185",
"_from": "actors/Carrie",
"_to": "movies/TheMatrixReloaded",
"_rev": "_ddo5tEe---",
"roles": [
"Trinity"
],
"year": 2003
},
{
"_key": "187",
"_id": "actsIn/187",
"_from": "actors/Laurence",
"_to": "movies/TheMatrixReloaded",
"_rev": "_ddo5tHK---",
"roles": [
"Morpheus"
],
"year": 2003
},
{
"_key": "189",
"_id": "actsIn/189",
"_from": "actors/Hugo",
"_to": "movies/TheMatrixReloaded",
"_rev": "_ddo5tJq---",
"roles": [
"Agent Smith"
],
"year": 2003
},
{
"_key": "192",
"_id": "actsIn/192",
"_from": "actors/Keanu",
"_to": "movies/TheMatrixRevolutions",
"_rev": "_ddo5tVq---",
"roles": [
"Neo"
],
"year": 2003
},
{
"_key": "194",
"_id": "actsIn/194",
"_from": "actors/Carrie",
"_to": "movies/TheMatrixRevolutions",
"_rev": "_ddo5tYC---",
"roles": [
"Trinity"
],
"year": 2003
},
{
"_key": "196",
"_id": "actsIn/196",
"_from": "actors/Laurence",
"_to": "movies/TheMatrixRevolutions",
"_rev": "_ddo5ta6---",
"roles": [
"Morpheus"
],
"year": 2003
},
{
"_key": "198",
"_id": "actsIn/198",
"_from": "actors/Hugo",
"_to": "movies/TheMatrixRevolutions",
"_rev": "_ddo5tfO---",
"roles": [
"Agent Smith"
],
"year": 2003
},
{
"_key": "203",
"_id": "actsIn/203",
"_from": "actors/Keanu",
"_to": "movies/TheDevilsAdvocate",
"_rev": "_ddo5tz6---",
"roles": [
"Kevin Lomax"
],
"year": 1997
},
{
"_key": "205",
"_id": "actsIn/205",
"_from": "actors/Charlize",
"_to": "movies/TheDevilsAdvocate",
"_rev": "_ddo5t3y---",
"roles": [
"Mary Ann Lomax"
],
"year": 1997
},
{
"_key": "207",
"_id": "actsIn/207",
"_from": "actors/Al",
"_to": "movies/TheDevilsAdvocate",
"_rev": "_ddo5t6m---",
"roles": [
"John Milton"
],
"year": 1997
},
{
"_key": "221",
"_id": "actsIn/221",
"_from": "actors/TomC",
"_to": "movies/AFewGoodMen",
"_rev": "_ddo5utq---",
"roles": [
"Lt. Daniel Kaffee"
],
"year": 1992
},
{
"_key": "223",
"_id": "actsIn/223",
"_from": "actors/JackN",
"_to": "movies/AFewGoodMen",
"_rev": "_ddo5uwS---",
"roles": [
"Col. Nathan R. Jessup"
],
"year": 1992
},
{
"_key": "225",
"_id": "actsIn/225",
"_from": "actors/DemiM",
"_to": "movies/AFewGoodMen",
"_rev": "_ddo5uzC---",
"roles": [
"Lt. Cdr. JoAnne Galloway"
],
"year": 1992
},
{
"_key": "227",
"_id": "actsIn/227",
"_from": "actors/KevinB",
"_to": "movies/AFewGoodMen",
"_rev": "_ddo5u2K---",
"roles": [
"Capt. Jack Ross"
],
"year": 1992
},
{
"_key": "229",
"_id": "actsIn/229",
"_from": "actors/KieferS",
"_to": "movies/AFewGoodMen",
"_rev": "_ddo5u5C---",
"roles": [
"Lt. Jonathan Kendrick"
],
"year": 1992
},
{
"_key": "231",
"_id": "actsIn/231",
"_from": "actors/NoahW",
"_to": "movies/AFewGoodMen",
"_rev": "_ddo5u7y---",
"roles": [
"Cpl. Jeffrey Barnes"
],
"year": 1992
},
{
"_key": "233",
"_id": "actsIn/233",
"_from": "actors/CubaG",
"_to": "movies/AFewGoodMen",
"_rev": "_ddo5vA----",
"roles": [
"Cpl. Carl Hammaker"
],
"year": 1992
},
{
"_key": "235",
"_id": "actsIn/235",
"_from": "actors/KevinP",
"_to": "movies/AFewGoodMen",
"_rev": "_ddo5vCm---",
"roles": [
"Lt. Sam Weinberg"
],
"year": 1992
},
{
"_key": "237",
"_id": "actsIn/237",
"_from": "actors/JTW",
"_to": "movies/AFewGoodMen",
"_rev": "_ddo5vGa---",
"roles": [
"Lt. Col. Matthew Andrew Markinson"
],
"year": 1992
},
{
"_key": "239",
"_id": "actsIn/239",
"_from": "actors/JamesM",
"_to": "movies/AFewGoodMen",
"_rev": "_ddo5vJi---",
"roles": [
"Pfc. Louden Downey"
],
"year": 1992
},
{
"_key": "241",
"_id": "actsIn/241",
"_from": "actors/ChristopherG",
"_to": "movies/AFewGoodMen",
"_rev": "_ddo5vMe---",
"roles": [
"Dr. Stone"
],
"year": 1992
},
{
"_key": "249",
"_id": "actsIn/249",
"_from": "actors/TomC",
"_to": "movies/TopGun",
"_rev": "_ddo5vka---",
"roles": [
"Maverick"
],
"year": 1986
},
{
"_key": "251",
"_id": "actsIn/251",
"_from": "actors/KellyM",
"_to": "movies/TopGun",
"_rev": "_ddo5vmu---",
"roles": [
"Charlie"
],
"year": 1986
},
{
"_key": "253",
"_id": "actsIn/253",
"_from": "actors/ValK",
"_to": "movies/TopGun",
"_rev": "_ddo5vou---",
"roles": [
"Iceman"
],
"year": 1986
},
{
"_key": "255",
"_id": "actsIn/255",
"_from": "actors/AnthonyE",
"_to": "movies/TopGun",
"_rev": "_ddo5vr----",
"roles": [
"Goose"
],
"year": 1986
},
{
"_key": "257",
"_id": "actsIn/257",
"_from": "actors/TomS",
"_to": "movies/TopGun",
"_rev": "_ddo5vtW---",
"roles": [
"Viper"
],
"year": 1986
},
{
"_key": "259",
"_id": "actsIn/259",
"_from": "actors/MegR",
"_to": "movies/TopGun",
"_rev": "_ddo5vva---",
"roles": [
"Carole"
],
"year": 1986
},
{
"_key": "269",
"_id": "actsIn/269",
"_from": "actors/TomC",
"_to": "movies/JerryMaguire",
"_rev": "_ddo5wQW---",
"roles": [
"Jerry Maguire"
],
"year": 2000
},
{
"_key": "271",
"_id": "actsIn/271",
"_from": "actors/CubaG",
"_to": "movies/JerryMaguire",
"_rev": "_ddo5wSm---",
"roles": [
"Rod Tidwell"
],
"year": 2000
},
{
"_key": "273",
"_id": "actsIn/273",
"_from": "actors/ReneeZ",
"_to": "movies/JerryMaguire",
"_rev": "_ddo5wV2---",
"roles": [
"Dorothy Boyd"
],
"year": 2000
},
{
"_key": "275",
"_id": "actsIn/275",
"_from": "actors/KellyP",
"_to": "movies/JerryMaguire",
"_rev": "_ddo5wZG---",
"roles": [
"Avery Bishop"
],
"year": 2000
},
{
"_key": "277",
"_id": "actsIn/277",
"_from": "actors/JerryO",
"_to": "movies/JerryMaguire",
"_rev": "_ddo5wbq---",
"roles": [
"Frank Cushman"
],
"year": 2000
},
{
"_key": "279",
"_id": "actsIn/279",
"_from": "actors/JayM",
"_to": "movies/JerryMaguire",
"_rev": "_ddo5weG---",
"roles": [
"Bob Sugar"
],
"year": 2000
},
{
"_key": "281",
"_id": "actsIn/281",
"_from": "actors/BonnieH",
"_to": "movies/JerryMaguire",
"_rev": "_ddo5wgq---",
"roles": [
"Laurel Boyd"
],
"year": 2000
},
{
"_key": "283",
"_id": "actsIn/283",
"_from": "actors/ReginaK",
"_to": "movies/JerryMaguire",
"_rev": "_ddo5wjm---",
"roles": [
"Marcee Tidwell"
],
"year": 2000
},
{
"_key": "285",
"_id": "actsIn/285",
"_from": "actors/JonathanL",
"_to": "movies/JerryMaguire",
"_rev": "_ddo5wo2---",
"roles": [
"Ray Boyd"
],
"year": 2000
},
{
"_key": "297",
"_id": "actsIn/297",
"_from": "actors/WilW",
"_to": "movies/StandByMe",
"_rev": "_ddo5xJu---",
"roles": [
"Gordie Lachance"
],
"year": 1986
},
{
"_key": "299",
"_id": "actsIn/299",
"_from": "actors/RiverP",
"_to": "movies/StandByMe",
"_rev": "_ddo5xNe---",
"roles": [
"Chris Chambers"
],
"year": 1986
},
{
"_key": "301",
"_id": "actsIn/301",
"_from": "actors/JerryO",
"_to": "movies/StandByMe",
"_rev": "_ddo5xP2---",
"roles": [
"Vern Tessio"
],
"year": 1986
},
{
"_key": "303",
"_id": "actsIn/303",
"_from": "actors/CoreyF",
"_to": "movies/StandByMe",
"_rev": "_ddo5xT----",
"roles": [
"Teddy Duchamp"
],
"year": 1986
},
{
"_key": "305",
"_id": "actsIn/305",
"_from": "actors/JohnC",
"_to": "movies/StandByMe",
"_rev": "_ddo5xVO---",
"roles": [
"Denny Lachance"
],
"year": 1986
},
{
"_key": "307",
"_id": "actsIn/307",
"_from": "actors/KieferS",
"_to": "movies/StandByMe",
"_rev": "_ddo5xX2---",
"roles": [
"Ace Merrill"
],
"year": 1986
},
{
"_key": "309",
"_id": "actsIn/309",
"_from": "actors/MarshallB",
"_to": "movies/StandByMe",
"_rev": "_ddo5xbC---",
"roles": [
"Mr. Lachance"
],
"year": 1986
},
{
"_key": "314",
"_id": "actsIn/314",
"_from": "actors/JackN",
"_to": "movies/AsGoodAsItGets",
"_rev": "_ddo5xuS---",
"roles": [
"Melvin Udall"
],
"year": 1997
},
{
"_key": "316",
"_id": "actsIn/316",
"_from": "actors/HelenH",
"_to": "movies/AsGoodAsItGets",
"_rev": "_ddo5xya---",
"roles": [
"Carol Connelly"
],
"year": 1997
},
{
"_key": "318",
"_id": "actsIn/318",
"_from": "actors/GregK",
"_to": "movies/AsGoodAsItGets",
"_rev": "_ddo5x3a---",
"roles": [
"Simon Bishop"
],
"year": 1997
},
{
"_key": "320",
"_id": "actsIn/320",
"_from": "actors/CubaG",
"_to": "movies/AsGoodAsItGets",
"_rev": "_ddo5x52---",
"roles": [
"Frank Sachs"
],
"year": 1997
},
{
"_key": "327",
"_id": "actsIn/327",
"_from": "actors/Robin",
"_to": "movies/WhatDreamsMayCome",
"_rev": "_ddo5yTK---",
"roles": [
"Chris Nielsen"
],
"year": 1998
},
{
"_key": "329",
"_id": "actsIn/329",
"_from": "actors/CubaG",
"_to": "movies/WhatDreamsMayCome",
"_rev": "_ddo5yWW---",
"roles": [
"Albert Lewis"
],
"year": 1998
},
{
"_key": "331",
"_id": "actsIn/331",
"_from": "actors/AnnabellaS",
"_to": "movies/WhatDreamsMayCome",
"_rev": "_ddo5yZG---",
"roles": [
"Annie Collins-Nielsen"
],
"year": 1998
},
{
"_key": "333",
"_id": "actsIn/333",
"_from": "actors/MaxS",
"_to": "movies/WhatDreamsMayCome",
"_rev": "_ddo5yb6---",
"roles": [
"The Tracker"
],
"year": 1998
},
{
"_key": "335",
"_id": "actsIn/335",
"_from": "actors/WernerH",
"_to": "movies/WhatDreamsMayCome",
"_rev": "_ddo5yei---",
"roles": [
"The Face"
],
"year": 1998
},
{
"_key": "341",
"_id": "actsIn/341",
"_from": "actors/EthanH",
"_to": "movies/SnowFallingonCedars",
"_rev": "_ddo5yzC---",
"roles": [
"Ishmael Chambers"
],
"year": 1999
},
{
"_key": "343",
"_id": "actsIn/343",
"_from": "actors/RickY",
"_to": "movies/SnowFallingonCedars",
"_rev": "_ddo5y12---",
"roles": [
"Kazuo Miyamoto"
],
"year": 1999
},
{
"_key": "345",
"_id": "actsIn/345",
"_from": "actors/MaxS",
"_to": "movies/SnowFallingonCedars",
"_rev": "_ddo5y4u---",
"roles": [
"Nels Gudmundsson"
],
"year": 1999
},
{
"_key": "347",
"_id": "actsIn/347",
"_from": "actors/JamesC",
"_to": "movies/SnowFallingonCedars",
"_rev": "_ddo5y7e---",
"roles": [
"Judge Fielding"
],
"year": 1999
},
{
"_key": "354",
"_id": "actsIn/354",
"_from": "actors/TomH",
"_to": "movies/YouveGotMail",
"_rev": "_ddo5zRO---",
"roles": [
"Joe Fox"
],
"year": 1998
},
{
"_key": "356",
"_id": "actsIn/356",
"_from": "actors/MegR",
"_to": "movies/YouveGotMail",
"_rev": "_ddo5zV----",
"roles": [
"Kathleen Kelly"
],
"year": 1998
},
{
"_key": "358",
"_id": "actsIn/358",
"_from": "actors/GregK",
"_to": "movies/YouveGotMail",
"_rev": "_ddo5zXy---",
"roles": [
"Frank Navasky"
],
"year": 1998
},
{
"_key": "360",
"_id": "actsIn/360",
"_from": "actors/ParkerP",
"_to": "movies/YouveGotMail",
"_rev": "_ddo5zaO---",
"roles": [
"Patricia Eden"
],
"year": 1998
},
{
"_key": "362",
"_id": "actsIn/362",
"_from": "actors/DaveC",
"_to": "movies/YouveGotMail",
"_rev": "_ddo5zd6---",
"roles": [
"Kevin Jackson"
],
"year": 1998
},
{
"_key": "364",
"_id": "actsIn/364",
"_from": "actors/SteveZ",
"_to": "movies/YouveGotMail",
"_rev": "_ddo5zgW---",
"roles": [
"George Pappas"
],
"year": 1998
},
{
"_key": "371",
"_id": "actsIn/371",
"_from": "actors/TomH",
"_to": "movies/SleeplessInSeattle",
"_rev": "_ddo5z8K---",
"roles": [
"Sam Baldwin"
],
"year": 1993
},
{
"_key": "373",
"_id": "actsIn/373",
"_from": "actors/MegR",
"_to": "movies/SleeplessInSeattle",
"_rev": "_ddo50-e---",
"roles": [
"Annie Reed"
],
"year": 1993
},
{
"_key": "375",
"_id": "actsIn/375",
"_from": "actors/RitaW",
"_to": "movies/SleeplessInSeattle",
"_rev": "_ddo50A6---",
"roles": [
"Suzy"
],
"year": 1993
},
{
"_key": "377",
"_id": "actsIn/377",
"_from": "actors/BillPull",
"_to": "movies/SleeplessInSeattle",
"_rev": "_ddo50DS---",
"roles": [
"Walter"
],
"year": 1993
},
{
"_key": "379",
"_id": "actsIn/379",
"_from": "actors/VictorG",
"_to": "movies/SleeplessInSeattle",
"_rev": "_ddo50Ga---",
"roles": [
"Greg"
],
"year": 1993
},
{
"_key": "381",
"_id": "actsIn/381",
"_from": "actors/RosieO",
"_to": "movies/SleeplessInSeattle",
"_rev": "_ddo50Jy---",
"roles": [
"Becky"
],
"year": 1993
},
{
"_key": "385",
"_id": "actsIn/385",
"_from": "actors/TomH",
"_to": "movies/JoeVersustheVolcano",
"_rev": "_ddo50Xe---",
"roles": [
"Joe Banks"
],
"year": 1990
},
{
"_key": "387",
"_id": "actsIn/387",
"_from": "actors/MegR",
"_to": "movies/JoeVersustheVolcano",
"_rev": "_ddo50e----",
"roles": [
"DeDe",
"Angelica Graynamore",
"Patricia Graynamore"
],
"year": 1990
},
{
"_key": "389",
"_id": "actsIn/389",
"_from": "actors/Nathan",
"_to": "movies/JoeVersustheVolcano",
"_rev": "_ddo50ga---",
"roles": [
"Baw"
],
"year": 1990
},
{
"_key": "395",
"_id": "actsIn/395",
"_from": "actors/BillyC",
"_to": "movies/WhenHarryMetSally",
"_rev": "_ddo50zC---",
"roles": [
"Harry Burns"
],
"year": 1998
},
{
"_key": "397",
"_id": "actsIn/397",
"_from": "actors/MegR",
"_to": "movies/WhenHarryMetSally",
"_rev": "_ddo501u---",
"roles": [
"Sally Albright"
],
"year": 1998
},
{
"_key": "399",
"_id": "actsIn/399",
"_from": "actors/CarrieF",
"_to": "movies/WhenHarryMetSally",
"_rev": "_ddo504S---",
"roles": [
"Marie"
],
"year": 1998
},
{
"_key": "433",
"_id": "actsIn/433",
"_from": "actors/BrunoK",
"_to": "movies/WhenHarryMetSally",
"_rev": "_ddo7CJe---",
"roles": [
"Jess"
],
"year": 1998
}
]
for d in data
INSERT d INTO actsIn

插入的数据如下

image-20211227155458904

使用命令

for v in actsIn return v

得到查询结果如下

image-20211227155613743

4.2 查询出演TheMatrix 或 TheDevilsAdvocate 的演员

4.2.1 查询出演TheMatrix的演员

查询命令如下

FOR x IN ANY 'movies/TheMatrix' actsIn
RETURN x

得到的结果如下

[
{
"_key": "Keanu",
"_id": "actors/Keanu",
"_rev": "_ddpPjj6--_",
"name": "Keanu Reeves",
"born": 1964
},
{
"_key": "Carrie",
"_id": "actors/Carrie",
"_rev": "_ddpPjj6--B",
"name": "Carrie-Anne Moss",
"born": 1967
},
{
"_key": "Laurence",
"_id": "actors/Laurence",
"_rev": "_ddpPjj6--D",
"name": "Laurence Fishburne",
"born": 1961
},
{
"_key": "Hugo",
"_id": "actors/Hugo",
"_rev": "_ddpPjj6--F",
"name": "Hugo Weaving",
"born": 1960
},
{
"_key": "Emil",
"_id": "actors/Emil",
"_rev": "_ddpPjj6--H",
"name": "Emil Eifrem",
"born": 1978
}
]

使用表格表示

image-20211227160331030

4.2.2 合并查询结果

使用 UNION_DISTINCT关键字合并查询结果

查询命令如下

FOR v IN UNION_DISTINCT(
( FOR x IN ANY 'movies/TheMatrix' actsIn RETURN x),
( FOR y IN ANY 'movies/TheDevilsAdvocate' actsIn RETURN y)
)
return v

得到的结果如下

image-20211227160714584

4.3 查询同时出演TheMatrix 或 TheDevilsAdvocate 的演员

这种情况下可以使用关键字INTERSECTION进行查询

FOR v IN INTERSECTION(
( FOR x IN ANY 'movies/TheMatrix' actsIn RETURN x),
( FOR y IN ANY 'movies/TheDevilsAdvocate' actsIn RETURN y)
)
return v

得到的查询结果如下

image-20211227161649341

4.4 查询两个演员同时出演的电影

这其实和关于movie1和movie2中共同演员的问题是一样的。 我们只需要改变起始顶点。 例如,让我们找出 Hugo Weaving 和Keanu Reeves 共同主演的所有电影:

FOR v IN INTERSECTION(
(FOR x IN ANY 'actors/Hugo' actsIn RETURN x),
(FOR y IN ANY 'actors/Keanu' actsIn RETURN y)
) RETURN v

得到的查询结果如下

image-20211227161955515

4.5 查询出演出过三部及以上电影的演员

将利用 AQL 的边缘索引和 COLLECT 语句进行分组。 基本思想是按起始顶点(在此数据集中始终是参与者)对所有边进行分组。 然后我们从结果中删除所有少于 3 部电影的演员。 下面的查询还返回计算出的演员演过的电影数量:

查询命令如下

FOR x IN actsIn
COLLECT actor = x._from WITH COUNT INTO counter
FILTER counter >= 3
RETURN { actor: actor, movies: counter }

得到的结果如下

[
{
"actor": "actors/Carrie",
"movies": 3
},
{
"actor": "actors/CubaG",
"movies": 4
},
{
"actor": "actors/Hugo",
"movies": 3
},
{
"actor": "actors/Keanu",
"movies": 4
},
{
"actor": "actors/Laurence",
"movies": 3
},
{
"actor": "actors/MegR",
"movies": 5
},
{
"actor": "actors/TomC",
"movies": 3
},
{
"actor": "actors/TomH",
"movies": 3
}
]

使用表格表示为

image-20211227162234783

4.6 查询出仅有6个演员的电影

与之前查询中的想法相同,但使用相等过滤器,但是现在我们需要电影而不是演员,因此我们返回 _to 属性:

FOR x IN actsIn
COLLECT movie = x._to WITH COUNT INTO counter
FILTER counter == 6
RETURN movie

查询结果如下

[
"movies/SleeplessInSeattle",
"movies/TopGun",
"movies/YouveGotMail"
]

4.7 根据电影查询演员数量

我们记得在我们的数据集中 _to 边缘对应于电影,因此我们计算相同 _to 出现的频率。 这是演员的数量。 该查询与之前的查询几乎相同,但在 COLLECT 之后没有 FILTER:

FOR x IN actsIn
COLLECT movie = x._to WITH COUNT INTO counter
RETURN { movie: movie, actors: counter }

得到结果如下

image-20211227162639476

4.8 根据演员查询电影的数量

边上的_to属性对应的是actor,所以我们按它分组,用COLLECT计数。 作为奖励,我们可以添加排序以首先返回拥有最多电影的演员:

FOR x IN actsIn
COLLECT actor = x._from WITH COUNT INTO counter
SORT counter DESC
RETURN { actor: actor, movies: counter }

得到的查询结果如下

image-20211227162828385

4.9 演员在两年内出演的电影数量

这个查询是多模型数据库真正发挥作用的地方。 首先我们想在生产中使用它,所以我们在 year 上设置了一个持久性索引。 这允许执行诸如 1990 和 1995 之间的快速范围查询。

db.actsIn.ensureIndex({ type: "persistent", fields: ["year"] });

然后执行

FOR x IN actsIn
FILTER x.year >= 1990 && x.year <= 1995
COLLECT actor = x._from WITH COUNT INTO counter
RETURN { actor: actor, movies: counter }

得到的结果如下

image-20211227163035896

4.10 根据演员姓名查询其电影年份和数量

如果我们想返回一个年份列表,而不仅仅是演员出演的电影数量,那么我们不能使用 COLLECT WITH COUNT INTO,因为我们只能在分组后访问演员和计数器。 相反,我们可以使用 COLLECT ... INTO来跟踪每个演员的电影年数。 年数等于电影的数量。

为简单起见,示例查询仅限于两个参与者。 作为额外的附加功能,它使用DOCUMENT() 函数查找演员姓名:

FOR x IN actsIn
FILTER x._from IN [ "actors/TomH", "actors/Keanu" ]
COLLECT actor = x._from INTO years = x.year
RETURN {
name: DOCUMENT(actor).name,
movies: COUNT(years),
years
}

查询结果如下

[
{
"name": "Keanu Reeves",
"movies": 4,
"years": [
1999,
2003,
2003,
1997
]
},
{
"name": "Tom Hanks",
"movies": 3,
"years": [
1998,
1993,
1990
]
}
]

使用表格表示为

image-20211227163400928