2017-04-09 178 views
0

購物車正在登錄到Neo4j。刪除過期關係

注意:每個購物車對訪問者都是唯一的,並且由購物車路徑(實際上是購物車ID cookie)定義。每個項目都是購物車中的一個lineitem。其獨特的推車和購物車中的產品(唯一的關鍵是item.key)。最後,product_id是指產品。

購物車包含需要更新的行,即使有人刪除行或更改數量。

設置更新現有行的值,但不會刪除更新後的購物車json到達時從購物車中刪除的行。

有沒有簡單的方法來修改這個查詢來自動刪除刪除的行?

UNWIND items as item 
MATCH (p:Cart {path:px.upath}) 
    SET p.total_price=cart.total_price 
MERGE (i:Item {key:item.key}) 
    SET 
      i.product_id=item.product_id, 
      i.quantity=item.quantity, 
MERGE (i)-[:LineOf]->(p) 

回答

0

我有點不解的查詢,因爲它好像你在項目正在合併爲他們添加到購物車中,相對於現有的產品配套上。此外,您添加的Item節點似乎是針對單個用戶的事務處理的(您正在根據輸入設置數量),因此在此處使用MERGE似乎不明智......如果兩個用戶試圖添加同一類型的物品在同一時間,但數量不同?其中一項交易將否決另一項...用戶1添加2項:項目a,但用戶2添加了10個項目a,這改變了用戶1:購物車現在閱讀10個:所選項目a(儘管:購物車的總數還沒有更新到...)看起來模型可以使用一些改進。

您可以這樣做的一種方法是將關係數量設置爲購物車而不是物品。

事情是這樣的:

UNWIND items as item 
MATCH (p:Cart {path:px.upath}) 
SET p.total_price=cart.total_price 
MERGE (i:Item {key:item.key}) 
// doesn't seem the place to set product_id, but leaving it in 
SET i.product_id=item.product_id 
MERGE (i)-[r:LineOf]->(p) 
SET r.quantity=item.quantity 

所以看起來像這樣的查詢是將項目添加到購物車,你需要刪除條目的查詢。

這應該做的伎倆:

// assume toDelete is your list of items to delete 
WITH toDelete 
MATCH (p:Cart {path:px.upath}) 
    SET p.total_price=cart.total_price 
MATCH (p)<-[r:LineOf]-(i) 
WHERE i.key in toDelete 
DELETE r 

如果您希望將設置你的項目,如在第一個查詢,並刪除不發送所有其他行,那麼這個組合查詢應該單查詢工作:

UNWIND items as item 
MATCH (p:Cart {path:px.upath}) 
SET p.total_price=cart.total_price 
// first delete all items from the cart that aren't listed 
OPTIONAL MATCH (p)<-[r:LineOf]-(i) 
WHERE NOT i.key in items 
DELETE r 
// now add/update the rest 
MERGE (i:Item {key:item.key}) 
// doesn't seem the place to set product_id, but leaving it in 
SET i.product_id=item.product_id 
MERGE (i)-[r:LineOf]->(p) 
SET r.quantity=item.quantity 
+0

增加了一個說明,以澄清結構的問題。我喜歡你的第二個查詢,並且會修改它以適合我的用例。謝謝! – fodon