2017-10-09 84 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)