简体   繁体   English

如何创建节点并在另一个新创建的节点上创建一对多关系?

[英]How can I create nodes and create a one-to-many relationship another newly created node?

    UNWIND $likedMovies AS likedMovie
    MERGE (lm:LikedMovie {
      id: likedMovie.id,
      name: likedMovie.name,
    })

    CREATE p = (:Person $person)-[:LIKES]->(lm)

This doesn't solve my problem as it creates a new Person for every likedMovie Instead I want the single Person to be related to all the likedMovie 's这并不能解决我的问题,因为它为每个likedMovie创建了一个新 Person 相反,我希望单个Person与所有likedMovie相关

I also tried this:我也试过这个:

    UNWIND $likedMovies AS likedMovie
    MERGE (lm:LikedMovie {
      id: likedMovie.id,
      name: likedMovie.name,
    })

    CREATE p = (:Person $person)

    FOREACH (lm in likedMovie | 
       MERGE (p)->[:LIKES]-(lm)
    )

But, it's giving me an error that p is a Path and needs to be a Node但是,它给了我一个错误p是一个路径并且需要是一个Node

When you assigned p = (:Person), it created a path with nodes in it.当您分配 p = (:Person) 时,它会创建一个包含节点的路径。 But if you assign a variable (p:Person), the variable p is the node itself.但是如果你分配一个变量 (p:Person),变量 p 就是节点本身。 Thus, you can use it to create your relationship with p one-to-many.因此,您可以使用它来创建与 p 一对多的关系。

CREATE (p:Person $person)
UNWIND $likedMovies AS likedMovie
WITH p, likedMovie
MERGE (p)-[:LIKES]->(likedMovie)

This template should do it (although I do not know what is in $person )这个模板应该可以做到(虽然我不知道$person中有什么)

  CREATE (p:Person {name: $person})

  FOREACH(m in $likedMovies |
      MERGE (lm:LikedMovie {id: m.id, name: m.name})
      MERGE (p)-[:LIKES]->(lm)
  )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据某些属性为新创建的节点与现有节点创建至少一个关系 - Create atmost one relationship for newly created node with existing node based on some property 如何基于父节点在两个随机节点之间创建关系? - How can I create a relationship between two random nodes based on parent node? 在 Neo4j 中,如何在一个节点与其他节点的集合之间创建关系? - In Neo4j how can I create relationships between one node an a collection of other nodes? 如何使用节点 ID 在两个现有节点之间创建关系? - How to create relationship between two existing nodes by using node id? 在2个新创建的节点之间创建关系 - Creating a relationship between 2 newly created nodes 您可以在一个查询中创建一个节点并将其链接到多个节点吗? - Can you create a node and link it to multiple nodes in one query? 我如何保证在事务中的两个现有节点之间只创建一个新的唯一节点 - How I guarantee I create only one new unique node between two existing nodes in a transaction 在从 UNWIND 列表创建的节点之间创建关系 - Create relationship between nodes created from UNWIND list 如何使用密码在数组中的项目和另一个节点之间创建关系 - How to use cypher to create a relationship between items in an array and another node 如何匹配和创建节点和关系? - How to MATCH and CREATE a node and relationship?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM