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

我想插入基于其他lebel/collection 。 我有2个lebel/collection单元,用户 ),他们之间有1个关系( Business ),我想根据他们的关系将数据插入到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' } 

有关WHERE子句的文档说:

WHERE将约束添加到MATCH或OPTIONAL MATCH子句中的模式,或者过滤WITH子句的结果。

那就是: WHERE不能和MERGE一起使用。

如注释中所述,您的查询应该将WHERE条件放在WITH子句之后,因为您可以使用WHERE筛选WITH结果。

 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)