根据Neo4j中的Where条件插入数据

时间:2017-10-09 04:54:26

标签: node.js neo4j cypher

我想根据其他lebel/collection插入数据。我有2 lebel/collection单位,用户),他们之间有1个关系(商家),我想将数据插入unit基于他们的关系。我的密码查询如下:

MATCH (u:Units)<-[:Business]-(s:Users)
WHERE s.id = 'some-user-id'

WITH count(u) as numOfUnit  

// return number of units connected with user
// if numOfUnit is smaller then 2 
// insert/merge new data into Units lebel/collection 
// with relation between them

MERGE ( bu:Units {name:'some-name-01', info:'some-info-01' })
WHERE numOfUnit < 2
ON CREATE SET
  bu.id = '${uuid()}',
  bu.created = '${moment().toISOString()}'
ON MATCH SET
  bu.updated = '${moment().toISOString()}'

WITH bu as bu 

MATCH ( bs:Users {id: 'some-user-id' })
MERGE (bs)-[r:Business]-(bu)

RETURN properties(bu)   

运行上述查询后,显示以下错误:

 { Neo4jError: Invalid input 'H': expected 'i/I' (line 10, column 18 
    (offset: 377))
        "       ON CREATE SET"
        ^
           at Neo4jError.Error (native)
           at new Neo4jError (../../../../node_modules/neo4j-driver/lib/v1/error.js:76:132)
           at newError (../../../../node_modules/neo4j-driver/lib/v1/error.js:66:10)
           at Connection._handleMessage (../../../../node_modules/neo4j-driver/lib/v1/internal/connector.js:355:56)
           at Dechunker.Connection._dechunker.onmessage (../../../../node_modules/neo4j-driver/lib/v1/internal/connector.js:286:12)
           at Dechunker._onHeader (../../../../node_modules/neo4j-driver/lib/v1/internal/chunking.js:246:14)
           at Dechunker.AWAITING_CHUNK (../../../../node_modules/neo4j-driver/lib/v1/internal/chunking.js:199:21)
           at Dechunker.write (../../../../node_modules/neo4j-driver/lib/v1/internal/chunking.js:257:28)
           at NodeChannel.self._ch.onmessage (../../../../node_modules/neo4j-driver/lib/v1/internal/connector.js:259:27)
           at TLSSocket.<anonymous> (../../../../node_modules/neo4j-driver/lib/v1/internal/ch-node.js:308:16)
       code: 'Neo.ClientError.Statement.SyntaxError',
       name: 'Neo4jError' }

1 个答案:

答案 0 :(得分:2)

关于WHERE条款的文档说:

  

WHERE 在MATCH或OPTIONAL MATCH中为模式添加约束   子句或过滤 WITH 子句的结果。

即:WHERE不能与MERGE一起使用。

正如评论中所述,您的查询应该在WHERE子句之后设置WITH条件,因为您可以使用WHERE来过滤WITH的结果。< / p>

MATCH (u:Units)<-[:Business]-(s:Users)
WHERE s.id = 'some-user-id'

WITH count(u) as numOfUnit  
WHERE numOfUnit < 2

MERGE ( bu:Units {name:'some-name-01', info:'some-info-01' })
ON CREATE SET
    bu.id = '${uuid()}',
    bu.created = '${moment().toISOString()}'
ON MATCH SET
    bu.updated = '${moment().toISOString()}'

WITH bu as bu 

MATCH ( bs:Users {id: 'some-user-id' })
MERGE (bs)-[r:Business]-(bu)

RETURN properties(bu)