问题
I'm currently trying to unwind a list of TravelEdges that has a "DateTime?" but I keep receiving the following error:
{"CypherTypeException: Type mismatch: expected a map but was String(\"2018-05-21T08:38:00\")"}
I'm currently using the latest version of neo4j (3.4.8) and was wondering if someone could assist?
Also, is there a more efficient way of adding the edges without having two matches? The Id's are unique.
List<TravelEdge> travelpoints = new List<TravelEdge>();
//Add stuff to list
graphClient.Cypher
.Unwind(travelpoints, "sc")
.Match("(s1:Node { Id: sc.Id1})")
.Match("(s2:Node { Id: sc.Id2})")
.Merge("(s1)-[t:Travels_To]->(s2)")
.OnCreate()
.Set("t.Time = sc.TravelTime")
.ExecuteWithoutResults();
public class Node{
//Unique
public long Id {get;set;}
}
public class Edge {
public DateTime? TravelTime {get;set;}
}
public class TravelEdge{
public long Id1 {get;set;}
public long Id2 {get;set;}
public DateTime? TravelTime {get;set;}
}
回答1:
@cypbersam is correct regarding your second query, but your sc
is a map
, as your TravelEdge
class is treated as a map
by the DB.
I've taken the code pretty much verbatim (see below) and when I run it with the GraphClient
or the BoltGraphClient
it works totally fine for me. So, I guess this could be a problem with the version of Neo4jClient
- what version are you using?
Working Code Below
Be careful if you copy / paste, I completely delete the DB in the first couple of lines
void Main()
{
//var graphClient = new GraphClient(new Uri("http://localhost:7474/db/data"), "neo4j", "neo");
var graphClient = new BoltGraphClient("bolt://localhost:7687", "neo4j", "neo");
graphClient.Connect();
graphClient.Cypher.Match("(n)").DetachDelete("n").ExecuteWithoutResults();
List<TravelEdge> travelpoints = new List<TravelEdge>{
new TravelEdge { Id1 = 1, Id2 = 2, TravelTime = new DateTime(2000,1,1) },
new TravelEdge { Id1 = 2, Id2 = 3, TravelTime = new DateTime(2000,1,2) },
new TravelEdge { Id1 = 3, Id2 = 4, TravelTime = new DateTime(2000,1,3) },
new TravelEdge { Id1 = 4, Id2 = 5, TravelTime = null },
};
var ids = new [] {1,2,3,4,5};
graphClient.Cypher
.Unwind(ids, "id")
.Merge("(a:Node {Id: id})")
.ExecuteWithoutResults();
//Add stuff to list
graphClient.Cypher
.Unwind(travelpoints, "sc")
.Match("(s1:Node { Id: sc.Id1})")
.Match("(s2:Node { Id: sc.Id2})")
.Merge("(s1)-[t:Travels_To]->(s2)")
.OnCreate()
.Set("t.Time = sc.TravelTime")
.ExecuteWithoutResults();
}
public class Node{
public long Id {get;set;}
}
public class TravelEdge {
public long Id1 {get;set;}
public long Id2 {get;set;}
public DateTime? TravelTime {get;set;}
}
回答2:
Each
sc
value is a String, but your query is trying to use it as if it is a map. That is,sc.Id1
andsc.Id2
do not make any sense whensc
is a String instead of a map. You need to change thetravelpoints
value so that it is a list of maps withId1
andId2
values.There is no way to ensure that the
t:Travels_To
relationship exists between 2 specific nodes without looking up those nodes. However, to help speed up the lookups, you can create an index or uniqueness constraint on:Node(Id)
.
来源:https://stackoverflow.com/questions/52680320/neo4j-client-unwind-with-datetime