how to use unwind in neo4jclient for updating data?

偶尔善良 提交于 2020-01-06 07:44:20

问题


I want to update some of my records in the neo4j DB. In order that I wrote this query in Neo4jClient in C#:

 _client.Cypher
            .Unwind(skills, "updatedSkill")
            .Match("(s : Skill {Name: updatedSkill.Name}")
            .Set("s = updatedSkill");
            .ExecuteWithoutResults();

Where skills is a simple list of Skill objects:

    public class Skill
        {
            public string Name { get; set; }
            public string Phrase { get; set; }          
        }

But this code throws exception when I call it. Its translated Cypher query is:

UNWIND [{
    "Name": "name1",
    "Phrase": "phrase1",
},{
    "Name": "name2",
    "Phrase": "phrase2",
}] AS updatedSkill
MATCH (s : Skill {Name: updatedSkill.Name}
SET s = updatedSkill

The exception is as follows:

     Invalid input '"': expected whitespace, comment, an identifier,
 UnsignedDecimalInteger, a property key name or '}' (line 3, column 5 (offset: 17))
    "    "Name": "name1","
         ^

When I remove the double-quotes of the properties Name and Phrase the query runs correctly. But I can do it because the query is auto-generated by Neo4jClient.

Any ideas?


回答1:


You have a typo in your MATCH statement. It is missing the closing ). If you correct this your query should work as expected. As an aside there is also an extra semi-colon at the end of your SET statement, which I have removed.

_client.Cypher
        .Unwind(skills, "updatedSkill")
        .Match("(s:Skill {Name: updatedSkill.Name})")
        .Set("s = updatedSkill")
        .ExecuteWithoutResults();

As an aside, this is an example where the query you are looking at it not truly representative of the parameterized query being sent to the Neo4j REST API. I assume that you obtained it by looking at _cypher.Query.DebugQueryText? For most situations this is just fine but where you are passing JSON objects it can lead to a misleading query due to the way it manipulates the string representations of the parameter objects. If you look at the actual exception being thrown by Neo4jClient it should inform you of the real cause of the issue. In this case

SyntaxException: Invalid input 'S': expected whitespace, comment, ')' or a relationship pattern... "SET s = updatedSkill"
 ^

This tells you the real problem is that you have not closed off your MATCH statement before starting the SET statement.



来源:https://stackoverflow.com/questions/34262667/how-to-use-unwind-in-neo4jclient-for-updating-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!