使用 APOC 在嵌套列表中创建 id 和 id 之间的关系
Create relationships between ids and ids in nested lists using APOC
我正在尝试使用 APOC 连接器在 Neo4J 中将 MongoDB 数据表示为图形,但我无法理解正确的语法。我在 mongodb 中的数据如下所示。
{
"_id" : ObjectId("5e88985f788e2ab63ff926d7"),
"role": "member",
"name": "Emmett Brown",
"dob" : "1955-03-19",
"registration_date" : "1985-10-26",
"follows" : []
},
{
"_id" : ObjectId("5e88985f788e2ab63ff926d8"),
"role": "member",
"name": "Marty McFly",
"dob" : "1968-06-09",
"registration_date" : "2015-10-26",
"follows": [{
"id" : [ObjectId("5e88985f788e2ab63ff926d7")]
}]
},
{
"_id" : ObjectId("5e88985f788e2ab63ff926d9"),
"role": "member",
"name": "Biff Tannen",
"dob" : "1959-04-15",
"registration_date" : "2006-09-15",
"follows": [{
"id" : [ObjectId("5e88985f788e2ab63ff926d7"), ObjectId("5e88985f788e2ab63ff926d8")]
}]
}
我想做的是在 Neo4J 中创建一个如下所示的图形:
CREATE (Emmett:Person)
CREATE (Marty:Person)
CREATE (Biff:Person)
CREATE
(Marty)-[:FOLLOWS]->(Emmett),
(Biff)-[:FOLLOWS]->(Emmett),
(Biff)-[:FOLLOWS]->(Marty)
所以换句话说,我想做的是使用 "follows" 键中的每个 ObjectId 作为目标节点。但是,由于我使用的是 ID,所以我不知道如何创建我的关系...这是我到目前为止的想法:
CALL apoc.mongodb.get('mongodb://localhost:27017', 'database_name', 'user_collection', {}) YIELD value AS person
MERGE (p:Person {name:person.name}) ON CREATE SET p.registration_date = person.registration_date
RETURN p
这允许我 return 我的所有节点并在 Neo4J 中显示它们,但过去 2 天我一直在尝试获取我的节点的值,但我做不到。 .. 所以我在想也许你们中的任何人都可以对此有所帮助吗?提前致谢!
我没有 Mongo 实例可以玩,所以用 JSON 文件模拟了这个 - 请注意,我已经将 ObjectId 位折叠成字符串,我认为这是如何Neo4j 处理它们。您需要将第一行替换为对 apoc.mongodb.get
的调用
[{
"_id" : "5e88985f788e2ab63ff926d7",
"role": "member",
"name": "Emmett Brown",
"dob" : "1955-03-19",
"registration_date" : "1985-10-26",
"follows" : []
},
{
"_id" : "5e88985f788e2ab63ff926d8",
"role": "member",
"name": "Marty McFly",
"dob" : "1968-06-09",
"registration_date" : "2015-10-26",
"follows": [{
"id" : ["5e88985f788e2ab63ff926d7"]
}]
},
{
"_id" : "5e88985f788e2ab63ff926d9",
"role": "member",
"name": "Biff Tannen",
"dob" : "1959-04-15",
"registration_date" : "2006-09-15",
"follows": [{
"id" : ["5e88985f788e2ab63ff926d7", "5e88985f788e2ab63ff926d8"]
}]
}
]
以下创建人物节点,然后运行第二遍尝试将它们连接在一起:
CALL apoc.load.json("example.json") YIELD value as person
WITH collect(person) as people
FOREACH (personDetails in people |
MERGE (p: Person { id: personDetails._id })
ON CREATE SET p.registrationDate = personDetails.registrationDate,
p.name = personDetails.name
)
WITH people
UNWIND people as personDetails
MATCH (follower: Person { id: personDetails._id })
UNWIND personDetails.follows as followsRecords
MATCH (followed: Person) WHERE followed.id in followsRecords.id
MERGE (follower)-[:FOLLOWS]->(followed)
我们可能还想在 Person.id 上创建一个唯一约束,这将加快处理大型数据集的速度,并防止在我们的查询错误时出现奇怪的数据问题:
CREATE CONSTRAINT ON (p:Person) ASSERT p.id IS UNIQUE
我正在尝试使用 APOC 连接器在 Neo4J 中将 MongoDB 数据表示为图形,但我无法理解正确的语法。我在 mongodb 中的数据如下所示。
{
"_id" : ObjectId("5e88985f788e2ab63ff926d7"),
"role": "member",
"name": "Emmett Brown",
"dob" : "1955-03-19",
"registration_date" : "1985-10-26",
"follows" : []
},
{
"_id" : ObjectId("5e88985f788e2ab63ff926d8"),
"role": "member",
"name": "Marty McFly",
"dob" : "1968-06-09",
"registration_date" : "2015-10-26",
"follows": [{
"id" : [ObjectId("5e88985f788e2ab63ff926d7")]
}]
},
{
"_id" : ObjectId("5e88985f788e2ab63ff926d9"),
"role": "member",
"name": "Biff Tannen",
"dob" : "1959-04-15",
"registration_date" : "2006-09-15",
"follows": [{
"id" : [ObjectId("5e88985f788e2ab63ff926d7"), ObjectId("5e88985f788e2ab63ff926d8")]
}]
}
我想做的是在 Neo4J 中创建一个如下所示的图形:
CREATE (Emmett:Person)
CREATE (Marty:Person)
CREATE (Biff:Person)
CREATE
(Marty)-[:FOLLOWS]->(Emmett),
(Biff)-[:FOLLOWS]->(Emmett),
(Biff)-[:FOLLOWS]->(Marty)
所以换句话说,我想做的是使用 "follows" 键中的每个 ObjectId 作为目标节点。但是,由于我使用的是 ID,所以我不知道如何创建我的关系...这是我到目前为止的想法:
CALL apoc.mongodb.get('mongodb://localhost:27017', 'database_name', 'user_collection', {}) YIELD value AS person
MERGE (p:Person {name:person.name}) ON CREATE SET p.registration_date = person.registration_date
RETURN p
这允许我 return 我的所有节点并在 Neo4J 中显示它们,但过去 2 天我一直在尝试获取我的节点的值,但我做不到。 .. 所以我在想也许你们中的任何人都可以对此有所帮助吗?提前致谢!
我没有 Mongo 实例可以玩,所以用 JSON 文件模拟了这个 - 请注意,我已经将 ObjectId 位折叠成字符串,我认为这是如何Neo4j 处理它们。您需要将第一行替换为对 apoc.mongodb.get
[{
"_id" : "5e88985f788e2ab63ff926d7",
"role": "member",
"name": "Emmett Brown",
"dob" : "1955-03-19",
"registration_date" : "1985-10-26",
"follows" : []
},
{
"_id" : "5e88985f788e2ab63ff926d8",
"role": "member",
"name": "Marty McFly",
"dob" : "1968-06-09",
"registration_date" : "2015-10-26",
"follows": [{
"id" : ["5e88985f788e2ab63ff926d7"]
}]
},
{
"_id" : "5e88985f788e2ab63ff926d9",
"role": "member",
"name": "Biff Tannen",
"dob" : "1959-04-15",
"registration_date" : "2006-09-15",
"follows": [{
"id" : ["5e88985f788e2ab63ff926d7", "5e88985f788e2ab63ff926d8"]
}]
}
]
以下创建人物节点,然后运行第二遍尝试将它们连接在一起:
CALL apoc.load.json("example.json") YIELD value as person
WITH collect(person) as people
FOREACH (personDetails in people |
MERGE (p: Person { id: personDetails._id })
ON CREATE SET p.registrationDate = personDetails.registrationDate,
p.name = personDetails.name
)
WITH people
UNWIND people as personDetails
MATCH (follower: Person { id: personDetails._id })
UNWIND personDetails.follows as followsRecords
MATCH (followed: Person) WHERE followed.id in followsRecords.id
MERGE (follower)-[:FOLLOWS]->(followed)
我们可能还想在 Person.id 上创建一个唯一约束,这将加快处理大型数据集的速度,并防止在我们的查询错误时出现奇怪的数据问题:
CREATE CONSTRAINT ON (p:Person) ASSERT p.id IS UNIQUE