Search code examples
c#neo4jneo4jclient

how to use unwind in neo4jclient for updating data?


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?


Solution

  • 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.