P2neo `on match`

I want to add some nodes, but using match, if a node exists, just update it's counter.

In normal cypher I can use the ON MATCH and ON CREATE as part of the MERGE command. How can I do something similar with the py2neo driver?

eg like

            node_query = f"""
                MERGE (nid:page{{name: "{page}" }})
                ON CREATE
                    SET
                        nid.name = "{page}",
                        nid.count = 1,
                        nid.flow = "{row['flow']}"
                ON MATCH
                    SET
                        nid.count = nid.count + 1
                RETURN nid.count
            """

as far as I can see py2neo has a match and merge as separate commands and can't be triggered during the merge/insert as above. I would have to go back over the whole graph and set individual values.

Py2neo provides a number of high level helpers which sit above Cypher. These are not intended however to form a full Cypher DSL, but instead to cater to the 80% use case, and help users get started without full Cypher knowledge.

To access more fine-grained functionality, such as you highlight here, you will definitely need to drop down into Cypher.

I believe there have been one or more efforts to build a full Cypher DSL for Python, but I'm not aware of their current status or level of completeness.

2 Likes