简体   繁体   中英

How to refactor the data in neo4j

Say I have a database with orders node as below

Order{OrderId, Customer, Date, Quantity, Product}

now I want to refactor this node in the database to look as below using a cypher query

(day)<-[:PLACED_ON]-(Order{OrderId, Quantity})-[:PLACED_BY]->(customer), (Order)-[:FOR_PRODUCT]->(product)

I understand that we can actually do such thing directly in the cypher, without having to load all the nodes in to my code and then make multiple cypher calls to the database.

Would it be possible for some one to help me understand how such refactoring can be done without introducing duplicates of customer, product and day node.

Rrgards Kiran

Yes, you can manipulate a Neo4j database with cypher.

Guessing that your current Order node looks similar to:

CREATE (:ORDER {orderId:100,customer:'John',date:13546354,quantity:1,product:'pizza'})

You could write the following:

MATCH (o:ORDER) 
CREATE (d:DAY{timestamp:o.date}) <- [:PLACED_ON] - o - [:PLACED_BY] -> (c:CUSTOMER{name:o.customer})
CREATE o - [:FOR_PRODUCT] -> (p:PRODUCT{name:o.product}) 
REMOVE o.product, o.customer, o.date 
RETURN o as order, d as day, c as customer, p as product

The query output would be:

Nodes created: 3
Relationships created: 3
Properties set: 6
Labels added: 3

Note that if you're having a large dataset, migrating an entire database can be very time consuming! You might want to try the PERIODIC COMMIT feature in the 2.1.0 milestone release.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM