2017-10-09 87 views
1

我想根据其他lebel/collection插入数据。我有2 lebel/collection单位,用户),他们之间有1个关系(业务),我想根据他们的关系插入数据到unit。下面我暗号查询给出:在Neo4j中插入基于Where条件的数据

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

该查询不编译,因为WHERE numOfUnit <2应该出现在MERGE之前。 –

回答

2

WHERE子句中的文档说:

WHERE adds constraints to the patterns in a MATCH or OPTIONAL MATCH clause or filters the results of a WITH clause.

即: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)