neo4j / neo4j-go-driver

Neo4j Bolt Driver for Go
Apache License 2.0
488 stars 68 forks source link

[Question] How to obtain the number of affected data after adding and modifying the cypher of node or relationship? #446

Closed edufriendchen closed 1 year ago

edufriendchen commented 1 year ago

Does the V5 version implement the function of returning cypher to affect the number of data? Explanation: Can you get [Set 2 properties] in the following figure after executing cypher. If it is implemented, please write a demo to tell me how to use it, because my ability to read documents in English is limited and there is little information about neo4j-go-driver on the Internet. screenshot-20230202-222311

fbiville commented 1 year ago

Hello and welcome! This kind of question is better suited to https://community.neo4j.com/ by the way. You can use the new ExecuteQuery released in 5.5.0 and get easy access to the ResultSummary as follows:

        // error handling omitted for brevity
    result, _ := neo4j.ExecuteQuery(ctx, driver,
        "CREATE (:FooBar {baz: true, another_one: 'bites the dust'})",
        nil,
        neo4j.EagerResultTransformer)
    summary := result.Summary
        // you can access many more counters with summary.Counters()
    propCount := summary.Counters().PropertiesSet()
    fmt.Printf("Set %d %s", propCount, pluralize("property", propCount))

If you do not want to use the experimental API yet (😢), you just to get a hold of a ResultWithContext (or the legacy variant Result) and call Consume:

    session := driver.NewSession(ctx, neo4j.SessionConfig{})
    // defer close session here
    // error handling omitted for brevity
    summary, _ := neo4j.ExecuteWrite[neo4j.ResultSummary](ctx, session, func(tx neo4j.ManagedTransaction) (neo4j.ResultSummary, error) {
        // error handling omitted for brevity
        result, _ := tx.Run(ctx, "CREATE (:FooBar {baz: true, another_one: 'bites the dust'})", nil)
        summary, _ := result.Consume(ctx)
        return summary, nil
    })
    propCount := summary.Counters().PropertiesSet()
    fmt.Printf("Set %d %s", propCount, pluralize("property", propCount))

You can also run neo4j.ExecuteRead[T] or session.Run in a similar way.

edufriendchen commented 1 year ago

Thank you very much for your help, I think I will write some Chinese blog, in order to help friends in China more easily learn it.