Skip to content

Latest commit

 

History

History
148 lines (112 loc) · 4.21 KB

QueryOperations.md

File metadata and controls

148 lines (112 loc) · 4.21 KB

AQL query operations

Query operations are focused on executing and analyzing AQL queries. These operations are accessible through Query property in database context object.

Query operation parameters

Applicable parameters available through fluent API:

  • Aql(string query) - Sets AQL query code.
  • BindVar(string key, object value) - Maps key/value bind parameter.
  • Count(bool value) - Determines whether the number of retrieved documents should be returned in Extra property of AResult instance. Default value: false.
  • Ttl(int value) - Determines whether the number of documents in the result set should be returned. Default value: false.
  • BatchSize(int value) - Determines maximum number of result documents to be transferred from the server to the client in one roundtrip. If not set this value is server-controlled.

Executing simple query

var db = new ADatabase("myDatabaseAlias");

var queryResult = db.Query
    .Aql(@"
    FOR item IN MyDocumentCollection 
        RETURN item
    ")
    .ToDocuments();
    
if (queryResult.Success)
{
    foreach (var document in queryResult.Value)
    {
        var foo = document.String("foo");
        var bar = document.Int("bar");
    }
}

Executing query with bind variables

var db = new ADatabase("myDatabaseAlias");

var queryResult = db.Query
    .BindVar("bar", 123)
    .Aql(@"
    FOR item IN MyDocumentCollection 
        FILTER item.bar == @bar
        RETURN item
    ")
    .ToDocuments();
    
if (queryResult.Success)
{
    foreach (var document in queryResult.Value)
    {
        var foo = document.String("foo");
        var bar = document.Int("bar");
    }
}

Executing non-query operation

Result of the non-query operation does not contain value information and is intended for queries that do not return any data.

var db = new ADatabase("myDatabaseAlias");

var queryResult = db.Query
    .Aql(@"
    UPSERT { bar: 1 }
    INSERT { foo: 'some string value', bar: 1 }
    UPDATE { foo: 'some string value updated', bar: 2 }
    IN MyCollection
    ")
    .ExecuteNonQuery();
    
if (queryResult.Success)
{
    // operation executed successfuly with queryResult.HasValue == false and queryResult.Value == null
}

Result format options

Query result can be retrieved through following methods with different value type:

  • ToDocuments() - Retrieves result value as list of documents.
  • ToList<T>() - Retrieves result value as list of generic objects.
  • ToList() - Retrieves result value as list of objects.
  • ToDocument() - Retrieves result value as single document.
  • ToObject<T>() - Retrieves result value as single generic object.
  • ToObject() - Retrieves result value as single object.

Parse query

Analyzes specified AQL query.

var db = new ADatabase("myDatabaseAlias");

var parseQueryResult = db.Query.Parse("FOR item IN MyDocumentCollection RETURN item");
    
if (parseQueryResult.Success)
{
    var analyzedQueryDocument = parseQueryResult.Value;
}

Minify query

Transforms specified query into minified version with removed leading and trailing whitespaces except new line characters.

var singleLineQuery = AQuery.Minify(@"
FOR item IN MyDocumentCollection
    RETURN item
");

Delete cursor

Deletes specified AQL query cursor.

var db = new ADatabase("myDatabaseAlias");

var deleteCursorResult = db.Query
    .DeleteCursor("someCursorID");
    
if (deleteCursorResult.Success)
{
    var isCursorDeleted = deleteCursorResult.Value;
}

More examples

More examples regarding AQL query operations can be found in unit tests.