问题
I want to create new properties for a pre-existing node based on a condition. To be more specific, I want to create a new node property when a node property matches with a property within the nested JSON array. my json structure looks something like this:
{"some tasks":[
{"id":1,"name":"John Doe"},
{"id":2,"name":"Jane Doe"}
],
"some links":[
{"id":1,"type":"cartoon"}
{"id":2,"type":"anime"}
]
}
I already created nodes with properties from tasks - now I want to iterate over the links part and update the properties of the node when the ids match. I tried using foreach like so-
call apoc.load.json("file:///precedence.json")yield value as line
foreach(link in line.link| match (n) where n.id=link.source)
return n
which returns the error-
Neo.ClientError.Statement.SyntaxError: Invalid use of MATCH inside FOREACH (line 2, column 28 (offset: 93))
"foreach(link in line.link| match (n) where n.id=link.source)"
so how do i check this condition inside foreach?
回答1:
You can't use a MATCH inside a FOREACH, it only allows updating clauses.
Instead you can UNWIND the list back into rows (you'll have a row per entry in the list) then you can MATCH and SET as needed.
I also highly recommend using labels in your query, and ensuring you have in index on the label and id property for fast lookups. For this example I'll use :Node as the label (so for the example you would have :Node(id) for the index):
CALL apoc.load.json("file:///precedence.json") yield value as line
UNWIND line.link as link
MATCH (n:Node)
WHERE n.id = link.source
SET n.type = link.type
来源:https://stackoverflow.com/questions/55548262/how-to-iterate-over-a-nested-json-array-and-change-the-node-properties-based-on