Skip to main content

Working with AQL

Query documents, one document at a time

ctx := context.Background()
query := "FOR d IN myCollection LIMIT 10 RETURN d"
cursor, err := db.Query(ctx, query, nil)
if err != nil {
// handle error
}
defer cursor.Close()
for {
var doc MyDocument
meta, err := cursor.ReadDocument(ctx, &doc)
if driver.IsNoMoreDocuments(err) {
break
} else if err != nil {
// handle other errors
}
fmt.Printf("Got doc with key '%s' from query\n", meta.Key)
}

Query documents, fetching the total count

ctx := driver.WithQueryCount(context.Background())
query := "FOR d IN myCollection RETURN d"
cursor, err := db.Query(ctx, query, nil)
if err != nil {
// handle error
}
defer cursor.Close()
fmt.Printf("Query yields %d documents\n", cursor.Count())

Query documents, with bind variables

ctx := driver.WithQueryCount(context.Background())
query := "FOR d IN myCollection FILTER d.name == @myVar RETURN d"
bindVars := map[string]interface{}{
"myVar": "Some name",
}
cursor, err := db.Query(ctx, query, bindVars)
if err != nil {
// handle error
}
defer cursor.Close()
fmt.Printf("Query yields %d documents\n", cursor.Count())
 
Help us improve

Anything unclear or buggy in this tutorial? Provide Feedback