Query the database
Once you have connected to the database, you can run queries using Cypher and the function ExecuteQuery()
.
ExecuteQuery() was introduced with the version 5.8 of the driver.For queries with earlier versions, use sessions and transactions. |
Write to the database
To create a node representing a person named Alice
, use the Cypher clause CREATE
:
Alice
result, err := neo4j.ExecuteQuery(ctx, driver,
"CREATE (p:Person {name: $name}) RETURN p", (1)
map[string]any{ (2)
"name": "Alice",
}, neo4j.EagerResultTransformer,
neo4j.ExecuteQueryWithDatabase("neo4j")) (3)
if err != nil {
panic(err)
}
summary := result.Summary (4)
fmt.Printf("Created %v nodes in %+v.\n",
summary.Counters().NodesCreated(),
summary.ResultAvailableAfter())
1 | The Cypher query |
2 | A map of query parameters |
3 | Which database the query should be run against |
4 | The summary of execution returned by the server |
Read from the database
To retrieve information from the database, use the Cypher clause MATCH
:
Person
nodesresult, err := neo4j.ExecuteQuery(ctx, driver,
"MATCH (p:Person) RETURN p.name AS name",
nil,
neo4j.EagerResultTransformer,
neo4j.ExecuteQueryWithDatabase("neo4j"))
if err != nil {
panic(err)
}
// Loop through results and do something with them
for _, record := range result.Records { (1)
name, _ := record.Get("name") // .Get() 2nd return is whether key is present
fmt.Println(name)
// or
// fmt.Println(record.AsMap()) // get Record as a map
}
// Summary information (2)
fmt.Printf("The query `%v` returned %v records in %+v.\n",
result.Summary.Query().Text(), len(result.Records),
result.Summary.ResultAvailableAfter())
1 | result.Records contains the result as an array of Record objects |
2 | result.Summary contains the summary of execution returned by the server |
When accessing a record’s content, all its properties are of type any .
This means that you have to cast them to the relevant Go type if you want to use methods/features defined on such types.
For example, if the name property coming from the database is a string, record.AsMap()["name"][1] would result in an invalid operation error at compilation time.
For it to work, cast the value to string before using it as a string: name := record.AsMap()["name"].(string) and then name[1] .
|
Update the database
Alice
to add an age
propertyresult, err := neo4j.ExecuteQuery(ctx, driver, `
MATCH (p:Person {name: $name})
SET p.age = $age
`, map[string]any{
"name": "Alice",
"age": 42,
}, neo4j.EagerResultTransformer,
neo4j.ExecuteQueryWithDatabase("neo4j"))
if err != nil {
panic(err)
}
fmt.Println("Query updated the database?",
result.Summary.Counters().ContainsUpdates())
To create a new relationship, linking it to two already existing node, use a combination of the Cypher clauses MATCH
and CREATE
:
:KNOWS
between Alice
and Bob
result, err := neo4j.ExecuteQuery(ctx, driver, `
MATCH (alice:Person {name: $name}) (1)
MATCH (bob:Person {name: $friend}) (2)
CREATE (alice)-[:KNOWS]->(bob) (3)
`, map[string]any{
"name": "Alice",
"friend": "Bob",
}, neo4j.EagerResultTransformer,
neo4j.ExecuteQueryWithDatabase("neo4j"))
if err != nil {
panic(err)
}
fmt.Println("Query updated the database?",
result.Summary.Counters().ContainsUpdates())
1 | Retrieve the person node named Alice and bind it to a variable alice |
2 | Retrieve the person node named Bob and bind it to a variable bob |
3 | Create a new :KNOWS relationship outgoing from the node bound to alice and attach to it the Person node named Bob |
Delete from the database
To remove a node and any relationship attached to it, use the Cypher clause DETACH DELETE
:
Alice
node and all its relationships// This does not delete _only_ p, but also all its relationships!
result, err := neo4j.ExecuteQuery(ctx, driver, `
MATCH (p:Person {name: $name})
DETACH DELETE p
`, map[string]any{
"name": "Alice",
}, neo4j.EagerResultTransformer,
neo4j.ExecuteQueryWithDatabase("neo4j"))
if err != nil {
panic(err)
}
fmt.Println("Query updated the database?",
result.Summary.Counters().ContainsUpdates())
Query parameters
Do not hardcode or concatenate parameters directly into queries. Instead, always use placeholders and specify the Cypher parameters, as shown in the previous examples. This is for:
-
performance benefits: Neo4j compiles and caches queries, but can only do so if the query structure is unchanged;
-
security reasons: see Protecting against Cypher Injection.
Query parameters should get grouped into a map and passed as second parameter to ExecuteQuery()
.
If a query has no parameters, you can pass nil
instead of an empty map.
parameters := map[string]any{
"name": "Alice",
"age": 42,
}
neo4j.ExecuteQuery(ctx, driver,
"MERGE (:Person {name: $name, age: $age})",
parameters,
neo4j.EagerResultTransformer,
neo4j.ExecuteQueryWithDatabase("neo4j"))
There can be circumstances where your query structure prevents the usage of parameters in all its parts. For those rare use cases, see Dynamic values in property keys, relationship types, and labels. |
Query configuration
You can supply further configuration parameters to alter the default behavior of ExecuteQuery()
.
These are provided as an arbitrary number of callbacks from the 4th function argument onward.
Database selection
It is recommended to always specify the database explicitly with the neo4j.ExecuteQueryWithDatabase("<dbName>")
callback, even on single-database instances.
This allows the driver to work more efficiently, as it saves a network round-trip to the server to resolve the home database.
If no database is given, the user’s home database is used.
neo4j.ExecuteQuery(ctx, driver,
"MATCH (p:Person) RETURN p.name",
nil,
neo4j.EagerResultTransformer,
neo4j.ExecuteQueryWithDatabase("neo4j"))
Specifying the database through the configuration method is preferred over the USE Cypher clause.
If the server runs on a cluster, queries with USE require server-side routing to be enabled.
Queries may also take longer to execute as they may not reach the right cluster member at the first attempt, and need to be routed to one containing the requested database.
|
Request routing
In a cluster environment, all queries are directed to the leader node by default.
To improve performance on read queries, you can use the callback neo4j.ExecuteQueryWithReadersRouting()
to route a query to the read nodes.
neo4j.ExecuteQuery(ctx, driver,
"MATCH (p:Person) RETURN p.name",
nil,
neo4j.EagerResultTransformer,
neo4j.ExecuteQueryWithDatabase("neo4j"),
neo4j.ExecuteQueryWithReadersRouting())
Although executing a write query in read mode likely results in a runtime error, you should not rely on this for access control. The difference between the two modes is that read transactions will be routed to any node of a cluster, whereas write ones will be directed to the leader. In other words, there is no guarantee that a write query submitted in read mode will be rejected. |
Run queries as a different user
You can execute a query through a different user with the configuration callback neo4j.ExecuteQueryWithAuthToken()
.
Switching user at the query level is cheaper than creating a new DriverWithContext
object.
The query is then run within the security context of the given user (i.e., home database, permissions, etc.).
Query-scoped authentication a server version >= 5.8.
queryAuth := neo4j.BasicAuth("somebodyElse", "theirPassword", "")
neo4j.ExecuteQuery(ctx, driver,
"MATCH (p:Person) RETURN p.name",
nil,
neo4j.EagerResultTransformer,
neo4j.ExecuteQueryWithDatabase("neo4j"),
neo4j.ExecuteQueryWithAuthToken(queryAuth))
The callback neo4j.ExecuteQueryWithImpersonatedUser()
provides a similar functionality, and is available in driver/server versions >= 4.4.
The difference is that you don’t need to know a user’s password to impersonate them, but the user under which the Driver
was created needs to have the appropriate permissions.
neo4j.ExecuteQuery(ctx, driver,
"MATCH (p:Person) RETURN p.name",
nil,
neo4j.EagerResultTransformer,
neo4j.ExecuteQueryWithDatabase("neo4j"),
neo4j.ExecuteQueryWithImpersonatedUser("<somebodyElse>"))
A full example
package main
import (
"fmt"
"context"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
)
func main() {
ctx := context.Background()
// Connection to database
dbUri := "<URI for Neo4j database>"
dbUser := "<Username>"
dbPassword := "<Password>"
driver, err := neo4j.NewDriverWithContext(
dbUri,
neo4j.BasicAuth(dbUser, dbPassword, ""))
if err != nil {
panic(err)
}
defer driver.Close(ctx)
err = driver.VerifyConnectivity(ctx)
if err != nil {
panic(err)
}
// Prepare data
people := []map[string]any {
{"name": "Alice", "age": 42, "friends": []string{"Bob", "Peter", "Anna"},},
{"name": "Bob", "age": 19,},
{"name": "Peter", "age": 50,},
{"name": "Anna", "age": 30,},
}
// Create some nodes
for _, person := range people {
_, err := neo4j.ExecuteQuery(ctx, driver,
"MERGE (p:Person {name: $person.name, age: $person.age})",
map[string]any{
"person": person,
}, neo4j.EagerResultTransformer,
neo4j.ExecuteQueryWithDatabase("neo4j"))
if err != nil {
panic(err)
}
}
// Create some relationships
for _, person := range people {
if person["friends"] != "" {
_, err := neo4j.ExecuteQuery(ctx, driver, `
MATCH (p:Person {name: $person.name})
UNWIND $person.friends AS friend_name
MATCH (friend:Person {name: friend_name})
MERGE (p)-[:KNOWS]->(friend)
`, map[string]any{
"person": person,
}, neo4j.EagerResultTransformer,
neo4j.ExecuteQueryWithDatabase("neo4j"))
if err != nil {
panic(err)
}
}
}
// Retrieve Alice's friends who are under 40
result, err := neo4j.ExecuteQuery(ctx, driver, `
MATCH (p:Person {name: $name})-[:KNOWS]-(friend:Person)
WHERE friend.age < $age
RETURN friend
`, map[string]any{
"name": "Alice",
"age": 40,
}, neo4j.EagerResultTransformer,
neo4j.ExecuteQueryWithDatabase("neo4j"))
if err != nil {
panic(err)
}
// Loop through results and do something with them
for _, record := range result.Records {
person, _ := record.Get("friend")
fmt.Println(person)
// or
// fmt.Println(record.AsMap())
}
// Summary information
fmt.Printf("\nThe query `%v` returned %v records in %+v.\n",
result.Summary.Query().Text(), len(result.Records),
result.Summary.ResultAvailableAfter())
}
For more information see API documentation → ExecuteQuery().
Glossary
- LTS
-
A Long Term Support release is one guaranteed to be supported for a number of years. Neo4j 4.4 is LTS, and Neo4j 5 will also have an LTS version.
- Aura
-
Aura is Neo4j’s fully managed cloud service. It comes with both free and paid plans.
- Cypher
-
Cypher is Neo4j’s graph query language that lets you retrieve data from the database. It is like SQL, but for graphs.
- APOC
-
Awesome Procedures On Cypher (APOC) is a library of (many) functions that can not be easily expressed in Cypher itself.
- Bolt
-
Bolt is the protocol used for interaction between Neo4j instances and drivers. It listens on port 7687 by default.
- ACID
-
Atomicity, Consistency, Isolation, Durability (ACID) are properties guaranteeing that database transactions are processed reliably. An ACID-compliant DBMS ensures that the data in the database remains accurate and consistent despite failures.
- eventual consistency
-
A database is eventually consistent if it provides the guarantee that all cluster members will, at some point in time, store the latest version of the data.
- causal consistency
-
A database is causally consistent if read and write queries are seen by every member of the cluster in the same order. This is stronger than eventual consistency.
- NULL
-
The null marker is not a type but a placeholder for absence of value. For more information, see Cypher → Working with
null
. - transaction
-
A transaction is a unit of work that is either committed in its entirety or rolled back on failure. An example is a bank transfer: it involves multiple steps, but they must all succeed or be reverted, to avoid money being subtracted from one account but not added to the other.
- backpressure
-
Backpressure is a force opposing the flow of data. It ensures that the client is not being overwhelmed by data faster than it can handle.
- transaction function
-
A transaction function is a callback executed by an
ExecuteRead
orExecuteWrite
call. The driver automatically re-executes the callback in case of server failure. - DriverWithContext
-
A
DriverWithContext
object holds the details required to establish connections with a Neo4j database.