Neo4j:建模多对多关系
Neo4j : Modeling Many to Many Relation
我如何为以下数据建模:
- “A 人”被“Studio 1”聘请出演“电影 A”
- “A 人”被“Studio 1”聘请出演“电影 B”
- “A 人”被“Studio 1”聘请出演“电影 C”
- “A 人”被“Studio 2”聘请出演“电影 X”
- “A 人”被“Studio 2”聘请出演“电影 Y”
- “A 人”被“Studio 2”聘请出演“Movie Z”
然后回答问题:“A 人”被“Studio 1”聘请出演了哪些电影?或找到“A 人”出演并被“Studio 1”聘用的电影?
1. CREATE (s1:Studio {name: 'Studio A'}),(s2:Studio {name: 'Studio B'})
2. CREATE (p:Actor {name: 'Person A'})
3. CREATE (m1:Movie {name:'Movie A'}),(m2:Movie {name:'Movie B'}),(m3:Movie {name:'Movie C'})
4. CREATE (m4:Movie {name:'Movie X'}),(m5:Movie {name:'Movie Y'}),(m6:Movie {name:'Movie Z'})
1. MATCH (A:Actor {name: 'Person A'}),(S:Studio {name: 'Studio A'}),(M:Movie {name: 'Movie A'})
MERGE (S)-[:HIRE]->(A)-[:ACT]->(M)
2. MATCH (A:Actor {name: 'Person A'}),(S:Studio {name: 'Studio A'}),(M:Movie {name: 'Movie B'})
MERGE (S)-[:HIRE]->(A)-[:ACT]->(M)
3. MATCH (A:Actor {name: 'Person A'}),(S:Studio {name: 'Studio A'}),(M:Movie {name: 'Movie C'})
MERGE (S)-[:HIRE]->(A)-[:ACT]->(M)
4. MATCH (A:Actor {name: 'Person A'}),(S:Studio {name: 'Studio B'}),(M:Movie {name: 'Movie X'})
MERGE (S)-[:HIRE]->(A)-[:ACT]->(M)
5. MATCH (A:Actor {name: 'Person A'}),(S:Studio {name: 'Studio B'}),(M:Movie {name: 'Movie Y'})
MERGE (S)-[:HIRE]->(A)-[:ACT]->(M)
6. MATCH (A:Actor {name: 'Person A'}),(S:Studio {name: 'Studio B'}),(M:Movie {name: 'Movie Z'})
MERGE (S)-[:HIRE]->(A)-[:ACT]->(M)
问题是,使用当前模型,您将失去关于工作室聘请演员在哪部电影中表演的上下文。想象一下,如果两个工作室聘请一个人在两部电影中表演。你最终会得到的是:
为了不丢失工作室雇用演员的电影的上下文,您可以引入一个中间节点作业,例如:
有了这个图形模型,您就不会忘记工作室雇用此人出演的电影的上下文。如果您需要一些有关基本 Cypher 查询的帮助,请查看 Cypher 培训课程:https://neo4j.com/graphacademy/training-querying-40/enrollment/
我如何为以下数据建模:
- “A 人”被“Studio 1”聘请出演“电影 A”
- “A 人”被“Studio 1”聘请出演“电影 B”
- “A 人”被“Studio 1”聘请出演“电影 C”
- “A 人”被“Studio 2”聘请出演“电影 X”
- “A 人”被“Studio 2”聘请出演“电影 Y”
- “A 人”被“Studio 2”聘请出演“Movie Z”
然后回答问题:“A 人”被“Studio 1”聘请出演了哪些电影?或找到“A 人”出演并被“Studio 1”聘用的电影?
1. CREATE (s1:Studio {name: 'Studio A'}),(s2:Studio {name: 'Studio B'})
2. CREATE (p:Actor {name: 'Person A'})
3. CREATE (m1:Movie {name:'Movie A'}),(m2:Movie {name:'Movie B'}),(m3:Movie {name:'Movie C'})
4. CREATE (m4:Movie {name:'Movie X'}),(m5:Movie {name:'Movie Y'}),(m6:Movie {name:'Movie Z'})
1. MATCH (A:Actor {name: 'Person A'}),(S:Studio {name: 'Studio A'}),(M:Movie {name: 'Movie A'})
MERGE (S)-[:HIRE]->(A)-[:ACT]->(M)
2. MATCH (A:Actor {name: 'Person A'}),(S:Studio {name: 'Studio A'}),(M:Movie {name: 'Movie B'})
MERGE (S)-[:HIRE]->(A)-[:ACT]->(M)
3. MATCH (A:Actor {name: 'Person A'}),(S:Studio {name: 'Studio A'}),(M:Movie {name: 'Movie C'})
MERGE (S)-[:HIRE]->(A)-[:ACT]->(M)
4. MATCH (A:Actor {name: 'Person A'}),(S:Studio {name: 'Studio B'}),(M:Movie {name: 'Movie X'})
MERGE (S)-[:HIRE]->(A)-[:ACT]->(M)
5. MATCH (A:Actor {name: 'Person A'}),(S:Studio {name: 'Studio B'}),(M:Movie {name: 'Movie Y'})
MERGE (S)-[:HIRE]->(A)-[:ACT]->(M)
6. MATCH (A:Actor {name: 'Person A'}),(S:Studio {name: 'Studio B'}),(M:Movie {name: 'Movie Z'})
MERGE (S)-[:HIRE]->(A)-[:ACT]->(M)
问题是,使用当前模型,您将失去关于工作室聘请演员在哪部电影中表演的上下文。想象一下,如果两个工作室聘请一个人在两部电影中表演。你最终会得到的是:
为了不丢失工作室雇用演员的电影的上下文,您可以引入一个中间节点作业,例如:
有了这个图形模型,您就不会忘记工作室雇用此人出演的电影的上下文。如果您需要一些有关基本 Cypher 查询的帮助,请查看 Cypher 培训课程:https://neo4j.com/graphacademy/training-querying-40/enrollment/