go-driver
go-driver copied to clipboard
AQL Queries not working with bindVars.
Environment:
- Golang ArangoDB driver ( "github.com/arangodb/go-driver v0.0.0-20200107125107-2a2392e62f69 ") * Go version ("go1.13.6 darwin/amd64")
- ArangoDB version ("3.6") .
Issue: driver.Database.Query() with bindVars is always empty. Verified same AQL query via ArangoDB, and it's successfully returned 2 documents.
Code Snipper:
` func (s *Service) searchProduct(offset int, count int) (interface{},int64, error){ //FOR doc IN explore SORT BM25(doc) DESC LIMIT 0,10 RETURN doc queryBuilder := query.NewForQuery(exploreViewName, "doc") qs := queryBuilder.SortBM25(true). LIMIT(offset, count). Return().String() s.logFactory.Fields().Done().Info(qs)
ctx := context.Background()
// Open database
db, err := s.dbClient.Database(ctx, databaseName)
if err != nil {
return nil, 0, errors.New(err, fmt.Sprintf("failed to open %s database", databaseName))
}
bindVars := map[string]interface{}{}
cursor, err := db.Query(ctx, qs, bindVars)
if err != nil {
return nil, 0, errors.New(err, fmt.Sprintf("failed to query [%s] on %s database", qs, databaseName))
}
defer cursor.Close()
s.logFactory.Fields().Done().Info(fmt.Sprintf("%d",cursor.Count()))
return bindVars, cursor.Count(),nil
} `
- bindVars is empty.
- cursor.Count() is 0
- However while debugging, I found cursor.cursorData.Result has 2 items in the list.
This is a blocker, given we can't execute a simple AQL query.
bindVars := map[string]interface{}{}
you have nothing tied down
bindVars["Key"]= "12345" ...
or
bindVars = map[string]interface{}{
"Lang": mylang,
"Company": "12345",,
"Key": key,
}
You need as example fullcount:
ctx := driver.WithQueryFullCount(ctx := context.Background())
you get the value with cursor.Statistics().FullCount()
bindVars does not contain the query result. As its name suggests, you specify @foo bind variables by passing in the map to Query.
You need to iterate over cursor.ReadDocument():
defer cursor.Close()
var results []interface{} // Use whatever type you want.
for cursor.HasMore() {
var result interface{}
if _, err := cursor.ReadDocument(ctx, &result); err != nil {
return err
}
results = append(results, result)
}
return results, nil