我想知道是否有人可以帮助我了解 Create Unique 在 Neo4J 中的实际工作原理。我正在尝试将多个地址注册到一个托运人节点,我希望城市、州和国家在这些地址中是相同的,以便它们都指向同一个节点。但是问题是创建唯一会导致数据库中同一节点的多个实例。
这是我必须向托运人注册地址的功能,我使用同一个托运人节点多次调用该地址,但不同的地址指向同一个城市。
public Node<Address> RegisterShipperAddress(Node<Shipper> shipper,AddressType addressType ,Address address, City city, State state, Country country)
{
var shipperRef = shipper.Reference;
var addressRef = neo4jClient.Cypher.Start(new { shipperNode = shipperRef }).CreateUnique("(shipperNode)-[r:HAS_ADDRESS { TypeName:{TypeName} }]->(addressRef{address})").WithParam("TypeName", addressType.TypeName).WithParam("address", address).Return<Node<Address>>("addressRef").Results.FirstOrDefault();
var cityRef = neo4jClient.Cypher.Start(new { addressNode = addressRef }).CreateUnique("(addressNode)-[:BELONGS_TO_CITY]->(cityRef{cityData})").WithParam("cityData", city).Return<Node<City>>("cityRef").Results.FirstOrDefault();
var stateRef = neo4jClient.Cypher.Start(new { cityNode = cityRef }).CreateUnique("(cityNode)-[:BELONGS_TO_STATE]->(stateRef{stateData})").WithParam("stateData", state).Return<Node<State>>("stateRef").Results.FirstOrDefault();
var countryRef = neo4jClient.Cypher.Start(new { stateNode = stateRef }).CreateUnique("(stateNode)-[:BELONGS_TO_COUNTRY]->(countryRef{stateData})").WithParam("stateData", state).Return<Node<Country>>("countryRef").Results.FirstOrDefault();
return addressRef;
}
现在假设我用不同的地址调用上述方法,如下所示:
//第一次调用....
RegisterShipperAddress(shipperNode,
new AddressType { TypeName = "REGISTRED" },
new Address
{
Address1 = "151/1 ABC Main",
Address2 = "My PO 1234",
Email = "abc@abc.com",
Mobile = "123456",
Phone = "123456"
},
new City { InternalId = 1, Label = "MyCity" },
new State { InternalId = 1, Label = "MyState" },
new Country { InternalId = 1, Label = "MyCountry" }
);
// 第二次调用
RegisterShipperAddress(shipperNode,
new AddressType { TypeName = "BILLING" },
new Address
{
Address1 = "1 X Main Road",
Address2 = "PO 555",
Email = "abc@abc.com",
Mobile = "123456",
Phone = "123456"
},
new City { InternalId = 1, Label = "MyCity" },
new State { InternalId = 1, Label = "MyState" },
new Country { InternalId = 1, Label = "MyCountry" }
);
我最终得到同一个节点的多个实例。只是想知道是否有可能使用 Cypher 的 Create Unique 子句来实现这一点。
这是我看到的那种图表。
--> (Address1)-->(MyCity)-->(MyState)-->(MyCountry)
(根)-->(发货人)||
--> (Address2)-->(MyCity)-->(MyState)-->(MyCountry)
还有什么是在一个事务中一次创建多个节点的最佳方法,我认为在上面的代码中,每个节点都是在一个单独的事务中创建的。