Document functions

AQL provides below listed functions to operate on objects / document values. Also see object access for additional language constructs.

ATTRIBUTES()

ATTRIBUTES(document, removeInternal, sort) → strArray

Return the top-level attribute keys of the document as an array. Optionally omit system attributes and sort the array.

  • document (object): an arbitrary document / object
  • removeInternal (bool, optional): whether all system attributes (_key, _id etc., every attribute key that starts with an underscore) shall be omitted in the result. The default is false.
  • sort (bool, optional): optionally sort the resulting array alphabetically. The default is false and will return the attribute names in any order.
  • returns strArray (array): the attribute keys of the input document as an array of strings

Examples

Return the attribute keys of an object:

RETURN ATTRIBUTES( { "foo": "bar", "_key": "123", "_custom": "yes" } )
Show query results
Hide query results
[
  [
    "foo",
    "_key",
    "_custom"
  ]
]

Return the attribute keys of an object but omit system attributes:

RETURN ATTRIBUTES( { "foo": "bar", "_key": "123", "_custom": "yes" }, true )
Show query results
Hide query results
[
  [
    "foo"
  ]
]

Return the attribute keys of an object in alphabetic order:

RETURN ATTRIBUTES( { "foo": "bar", "_key": "123", "_custom": "yes" }, false, true )
Show query results
Hide query results
[
  [
    "_custom",
    "_key",
    "foo"
  ]
]

Complex example to count how often every top-level attribute key occurs in the documents of a collection (expensive on large collections):

LET attributesPerDocument = (
    FOR doc IN collection RETURN ATTRIBUTES(doc, true)
)
FOR attributeArray IN attributesPerDocument
    FOR attribute IN attributeArray
        COLLECT attr = attribute WITH COUNT INTO count
        SORT count DESC
        RETURN {attr, count}

COUNT()

This is an alias for LENGTH().

HAS()

HAS(document, attributeName) → isPresent

Test whether an attribute is present in the provided document.

  • document (object): an arbitrary document / object
  • attributeName (string): the attribute key to test for
  • returns isPresent (bool): true if document has an attribute named attributeName, and false otherwise. Also returns true if the attribute has a falsy value (null, 0, false, empty string "")

The function checks if the specified attribute exists, regardless of its value. Other ways of testing for the existence of an attribute may behave differently if the attribute has a falsy value or is not present (implicitly null on object access):

!!{ name: "" }.name        // false
HAS( { name: "" }, "name") // true

{ name: null }.name == null   // true
{ }.name == null              // true
HAS( { name: null }, "name" ) // true
HAS( { }, "name" )            // false

Note that HAS() can not utilize indexes. If it is not necessary to distinguish between explicit and implicit null values in your query, you may use an equality comparison to test for null and create a non-sparse index on the attribute you want to test against:

FILTER !HAS(doc, "name")    // can not use indexes
FILTER IS_NULL(doc, "name") // can not use indexes
FILTER doc.name == null     // can utilize non-sparse indexes

Examples

Check whether the example object has a name attribute key:

RETURN HAS( { name: "Jane" }, "name" )
Show query results
Hide query results
[
  true
]

Check whether the example object has an age attribute key:

RETURN HAS( { name: "Jane" }, "age" )
Show query results
Hide query results
[
  false
]

Falsy attribute values like null still count as the attribute being present:

RETURN HAS( { name: null }, "name" )
Show query results
Hide query results
[
  true
]

IS_SAME_COLLECTION()

IS_SAME_COLLECTION(collectionName, documentHandle) → isSame

Test whether the documentHandle has collectionName as collection.

The function does not validate whether the collection actually contains the specified document. It only compares the name of the specified collection with the collection name part of the specified document.

  • collectionName (string): the name of a collection as string
  • documentHandle (string|object): a document identifier string (e.g. _users/1234) or an object with an _id attribute (e.g. a document from a collection).
  • returns isSame (bool): true if the collection of documentHandle is the same as collectionName, or false if it is not. If documentHandle is an object without an _id attribute or anything other than a string or object, then null is returned and a warning is raised.

Examples

RETURN [
    IS_SAME_COLLECTION( "_users", "_users/my-user" ),
    IS_SAME_COLLECTION( "_users", { _id: "_users/my-user" } ),
    IS_SAME_COLLECTION( "_users", "foobar/baz"),
    IS_SAME_COLLECTION( "_users", { _id: "something/else" } )
  ]
Show query results
Hide query results
[
  [
    true,
    true,
    false,
    false
  ]
]

KEEP()

KEEP(document, attributeName1, attributeName2, ... attributeNameN) → doc

Keep only the attributes attributeName to attributeNameN of document. All other attributes will be removed from the result.

To do the opposite, see UNSET().

  • document (object): a document / object
  • attributeNames (string, repeatable): an arbitrary number of attribute names as multiple arguments
  • returns doc (object): a document with only the specified attributes at the top-level

Examples

Keep the top-level foo attribute, preserving its nested object:

LET doc = { foo: { bar: { foo: 1, baz: 2 }, baz: 3 }, baz: 4 }
  RETURN KEEP(doc, "foo")
Show query results
Hide query results
[
  {
    "foo": {
      "bar": {
        "foo": 1,
        "baz": 2
      },
      "baz": 3
    }
  }
]

Keep the top-level bar attribute, which the example object does not have, resulting in an empty object:

LET doc = { foo: { bar: { foo: 1, baz: 2 }, baz: 3 }, baz: 4 }
  RETURN KEEP(doc, "bar")
Show query results
Hide query results
[
  {}
]

Keep the top-level baz attribute:

LET doc = { foo: { bar: { foo: 1, baz: 2 }, baz: 3 }, baz: 4 }
  RETURN KEEP(doc, "baz")
Show query results
Hide query results
[
  {
    "baz": 4
  }
]

Keep multiple top-level attributes (foo and baz):

LET doc = { foo: { bar: { foo: 1, baz: 2 }, baz: 3 }, baz: 4 }
  RETURN KEEP(doc, "foo", "baz")
Show query results
Hide query results
[
  {
    "baz": 4,
    "foo": {
      "bar": {
        "foo": 1,
        "baz": 2
      },
      "baz": 3
    }
  }
]

KEEP(document, attributeNameArray) → doc

  • document (object): a document / object
  • attributeNameArray (array): an array of attribute names as strings
  • returns doc (object): a document with only the specified attributes at the top-level

Examples

Keep multiple top-level attributes (foo and baz), by passing an array of the attribute keys instead of individual arguments:

LET doc = { foo: { bar: { foo: 1, baz: 2 }, baz: 3 }, baz: 4 }
  RETURN KEEP(doc, ["foo", "baz"])
Show query results
Hide query results
[
  {
    "baz": 4,
    "foo": {
      "bar": {
        "foo": 1,
        "baz": 2
      },
      "baz": 3
    }
  }
]

KEEP_RECURSIVE()

KEEP_RECURSIVE(document, attributeName1, attributeName2, ... attributeNameN) → doc

Recursively preserve the attributes attributeName1 to attributeNameN from document and its sub-documents. All other attributes will be removed.

To do the opposite, use UNSET_RECURSIVE().

  • document (object): a document / object
  • attributeNames (string, repeatable): an arbitrary number of attribute names as multiple arguments (at least 1)
  • returns doc (object): document with only the specified attributes at all levels (top-level as well as nested objects)

Examples

Recursively preserve foo attributes, but not nested attributes that have parents with other names:

LET doc = { foo: { bar: { foo: 1, baz: 2 }, baz: 3 }, baz: 4 }
  RETURN KEEP_RECURSIVE(doc, "foo")
Show query results
Hide query results
[
  {
    "foo": {}
  }
]

Recursively preserve bar attributes, but there is none at the top-level, leading to an empty object:

LET doc = { foo: { bar: { foo: 1, baz: 2 }, baz: 3 }, baz: 4 }
  RETURN KEEP_RECURSIVE(doc, "bar")
Show query results
Hide query results
[
  {}
]

Recursively preserve baz attributes, but not nested attributes that have parents with other names:

LET doc = { foo: { bar: { foo: 1, baz: 2 }, baz: 3 }, baz: 4 }
  RETURN KEEP_RECURSIVE(doc, "baz")
Show query results
Hide query results
[
  {
    "baz": 4
  }
]

Recursively preserve multiple attributes (foo and bar):

LET doc = { foo: { bar: { foo: 1, baz: 2 }, baz: 3 }, baz: 4 }
  RETURN KEEP_RECURSIVE(doc, "foo", "bar")
Show query results
Hide query results
[
  {
    "foo": {
      "bar": {
        "foo": 1
      }
    }
  }
]

Recursively preserve multiple attributes (foo and baz), but not nested attributes that have parents with other names:

LET doc = { foo: { bar: { foo: 1, baz: 2 }, baz: 3 }, baz: 4 }
  RETURN KEEP_RECURSIVE(doc, "foo", "baz")
Show query results
Hide query results
[
  {
    "baz": 4,
    "foo": {
      "baz": 3
    }
  }
]

Recursively preserve multiple attributes (foo, bar, and baz), preserving all attributes of the example object:

LET doc = { foo: { bar: { foo: 1, baz: 2 }, baz: 3 }, baz: 4 }
  RETURN KEEP_RECURSIVE(doc, "foo", "bar", "baz")
Show query results
Hide query results
[
  {
    "baz": 4,
    "foo": {
      "bar": {
        "baz": 2,
        "foo": 1
      },
      "baz": 3
    }
  }
]

KEEP_RECURSIVE(document, attributeNameArray) → doc

  • document (object): a document / object
  • attributeNameArray (array): an array of attribute names as strings
  • returns doc (object): document with only the specified attributes at all levels (top-level as well as nested objects)

Examples

Recursively preserve multiple attributes (foo and baz), by passing an array of the attribute keys instead of individual arguments:

LET doc = { foo: { bar: { foo: 1, baz: 2 }, baz: 3 }, baz: 4 }
  RETURN KEEP_RECURSIVE(doc, ["foo", "baz"])
Show query results
Hide query results
[
  {
    "baz": 4,
    "foo": {
      "baz": 3
    }
  }
]

LENGTH()

LENGTH(doc) → attrCount

Determine the number of attribute keys of an object / document.

LENGTH() can also determine the number of elements in an array, the amount of documents in a collection and the character length of a string.

  • doc (object): a document / object
  • returns attrCount (number): the number of attribute keys in doc, regardless of their values

Examples

RETURN LENGTH({ name: "Emma", age: 36, phone: { mobile: "..." } })
Show query results
Hide query results
[
  3
]

MATCHES()

MATCHES(document, examples, returnIndex) → match

Compare the given document against each example document provided. The comparisons will be started with the first example. All attributes of the example will be compared against the attributes of document. If all attributes match, the comparison stops and the result is returned. If there is a mismatch, the function will continue the comparison with the next example until there are no more examples left.

The examples can be an array of 1..n example documents or a single document, with any number of attributes each.

An attribute value of null will match documents with an explicit attribute value of null as well as documents with this attribute missing (implicitly null). Only HAS() can differentiate between an attribute being absent and having a stored null value.

An empty object {} will match all documents. Be careful not to ask for all documents accidentally. For example, the arangojs driver skips attributes with a value of undefined, turning {attr: undefined} into {}.

MATCHES() can not utilize indexes. You may use plain FILTER conditions instead to potentially benefit from existing indexes:

FOR doc IN coll
  FILTER (cond1 AND cond2 AND cond3) OR (cond4 AND cond5) ...
  • document (object): document to determine whether it matches any example
  • examples (object|array): a single document, or an array of documents to compare against. Specifying an empty array is not allowed.
  • returnIndex (bool): by setting this flag to true, the index of the example that matched will be returned (starting at offset 0), or -1 if there was no match. The default is false and makes the function return a boolean.
  • returns match (bool|number): if document matches one of the examples, true is returned, otherwise false. A number is returned instead if returnIndex is enabled.

Examples

Check whether all attributes of the example are present in the document:

LET doc = {
    name: "jane",
    age: 27,
    active: true
  }
  RETURN MATCHES(doc, { age: 27, active: true } )
Show query results
Hide query results
[
  true
]

Check whether one of the examples matches the document and return the index of the matching example:

RETURN MATCHES(
    { "test": 1 },
    [
      { "test": 1, "foo": "bar" },
      { "foo": 1 },
      { "test": 1 }
    ],
  true)
Show query results
Hide query results
[
  2
]

MERGE()

MERGE(document1, document2, ... documentN) → mergedDocument

Merge the documents document1 to documentN into a single document. If document attribute keys are ambiguous, the merged result will contain the values of the documents contained later in the argument list.

Note that merging will only be done for top-level attributes. If you wish to merge sub-attributes, use MERGE_RECURSIVE() instead.

  • documents (object, repeatable): an arbitrary number of documents as multiple arguments (at least 2)
  • returns mergedDocument (object): a combined document

Examples

Two documents with distinct attribute names can easily be merged into one:

RETURN MERGE(
    { "user1": { "name": "Jane" } },
    { "user2": { "name": "Tom" } }
  )
Show query results
Hide query results
[
  {
    "user1": {
      "name": "Jane"
    },
    "user2": {
      "name": "Tom"
    }
  }
]

When merging documents with identical attribute names, the attribute values of the latter documents will be used in the end result:

RETURN MERGE(
    { "users": { "name": "Jane" } },
    { "users": { "name": "Tom" } }
  )
Show query results
Hide query results
[
  {
    "users": {
      "name": "Tom"
    }
  }
]

MERGE(docArray) → mergedDocument

MERGE() also accepts a single array parameter. This variant allows combining the attributes of multiple objects in an array into a single object.

  • docArray (array): an array of documents, as sole argument
  • returns mergedDocument (object): a combined document

Examples

RETURN MERGE(
    [
      { foo: "bar" },
      { quux: "quetzalcoatl", ruled: true },
      { bar: "baz", foo: "done" }
    ]
  )
Show query results
Hide query results
[
  {
    "foo": "done",
    "quux": "quetzalcoatl",
    "ruled": true,
    "bar": "baz"
  }
]

MERGE_RECURSIVE()

MERGE_RECURSIVE(document1, document2, ... documentN) → mergedDocument

Recursively merge the documents document1 to documentN into a single document. If document attribute keys are ambiguous, the merged result will contain the values of the documents contained later in the argument list.

  • documents (object, repeatable): an arbitrary number of documents as multiple arguments (at least 2)
  • returns mergedDocument (object): a combined document

MERGE_RECURSIVE() does not support the single array parameter variant that MERGE() offers.

Examples

Two documents with distinct attribute names can easily be merged into one:

RETURN MERGE_RECURSIVE(
    { "user-1": { "name": "Jane", "livesIn": { "city": "LA" } } },
    { "user-1": { "age": 42, "livesIn": { "state": "CA" } } }
  )
Show query results
Hide query results
[
  {
    "user-1": {
      "livesIn": {
        "city": "LA",
        "state": "CA"
      },
      "name": "Jane",
      "age": 42
    }
  }
]

PARSE_IDENTIFIER()

PARSE_IDENTIFIER(documentHandle) → parts

Parse a document handle and return its individual parts as separate attributes.

This function can be used to easily determine the collection name and key of a given document.

  • documentHandle (string|object): a document identifier string (e.g. _users/1234) or a regular document from a collection. Passing either a non-string or a non-document or a document without an _id attribute will result in an error.
  • returns parts (object): an object with the attributes collection and key

Examples

Parse a document identifier string:

RETURN PARSE_IDENTIFIER("_users/my-user")
Show query results
Hide query results
[
  {
    "collection": "_users",
    "key": "my-user"
  }
]

Parse the document identifier string of a document (_id attribute):

RETURN PARSE_IDENTIFIER( { "_id": "mycollection/mykey", "value": "some value" } )
Show query results
Hide query results
[
  {
    "collection": "mycollection",
    "key": "mykey"
  }
]

TRANSLATE()

TRANSLATE(value, lookupDocument, defaultValue) → mappedValue

Look up the specified value in the lookupDocument. If value is a key in lookupDocument, then value will be replaced with the lookup value found. If value is not present in lookupDocument, then defaultValue will be returned if specified. If no defaultValue is specified, value will be returned unchanged.

  • value (string): the value to encode according to the mapping
  • lookupDocument (object): a key/value mapping as document
  • defaultValue (any, optional): a fallback value in case value is not found
  • returns mappedValue (any): the encoded value, or the unaltered value or defaultValue (if supplied) in case it could not be mapped

Examples

Translate a country code to a country name:

RETURN TRANSLATE("FR", { US: "United States", UK: "United Kingdom", FR: "France" } )
Show query results
Hide query results
[
  "France"
]

The unaltered input value is returned if no match is found in the mapping:

RETURN TRANSLATE(42, { foo: "bar", bar: "baz" } )
Show query results
Hide query results
[
  42
]

If you specify a fallback value and no match is found in the mapping, then the fallback value returned instead of the input value:

RETURN TRANSLATE(42, { foo: "bar", bar: "baz" }, "not found!")
Show query results
Hide query results
[
  "not found!"
]

Note that any non-string input value is implicitly cast to a string before the lookup:

RETURN TRANSLATE(42, { "42": true } )
Show query results
Hide query results
[
  true
]

UNSET()

UNSET(document, attributeName1, attributeName2, ... attributeNameN) → doc

Remove the attributes attributeName1 to attributeNameN from document. All other attributes will be preserved.

To do the opposite, see KEEP().

  • document (object): a document / object
  • attributeNames (string, repeatable): an arbitrary number of attribute names as multiple arguments (at least 1)
  • returns doc (object): document without the specified attributes at the top-level

Examples

Remove the top-level foo attribute, including its nested objects:

LET doc = { foo: { bar: { foo: 1, baz: 2 }, baz: 3 }, baz: 4 }
  RETURN UNSET(doc, "foo")
Show query results
Hide query results
[
  {
    "baz": 4
  }
]

Remove the top-level bar attribute, which the example object does not have, resulting in an unchanged object:

LET doc = { foo: { bar: { foo: 1, baz: 2 }, baz: 3 }, baz: 4 }
  RETURN UNSET(doc, "bar")
Show query results
Hide query results
[
  {
    "baz": 4,
    "foo": {
      "bar": {
        "foo": 1,
        "baz": 2
      },
      "baz": 3
    }
  }
]

Remove the top-level baz attribute:

LET doc = { foo: { bar: { foo: 1, baz: 2 }, baz: 3 }, baz: 4 }
  RETURN UNSET(doc, "baz")
Show query results
Hide query results
[
  {
    "foo": {
      "bar": {
        "foo": 1,
        "baz": 2
      },
      "baz": 3
    }
  }
]

Remove multiple top-level attributes (foo and baz), resulting in an empty object in this example:

LET doc = { foo: { bar: { foo: 1, baz: 2 }, baz: 3 }, baz: 4 }
  RETURN UNSET(doc, "foo", "baz")
Show query results
Hide query results
[
  {}
]

UNSET(document, attributeNameArray) → doc

  • document (object): a document / object
  • attributeNameArray (array): an array of attribute names as strings
  • returns doc (object): document without the specified attributes at the top-level

Examples

Remove multiple top-level attributes (foo and baz), by passing an array of the attribute keys instead of individual arguments:

LET doc = { foo: { bar: { foo: 1, baz: 2 }, baz: 3 }, baz: 4 }
  RETURN UNSET(doc, ["foo", "bar"])
Show query results
Hide query results
[
  {
    "baz": 4
  }
]

UNSET_RECURSIVE()

UNSET_RECURSIVE(document, attributeName1, attributeName2, ... attributeNameN) → doc

Recursively remove the attributes attributeName1 to attributeNameN from document and its sub-documents. All other attributes will be preserved.

To do the opposite, use KEEP_RECURSIVE().

  • document (object): a document / object
  • attributeNames (string, repeatable): an arbitrary number of attribute names as multiple arguments (at least 1)
  • returns doc (object): document without the specified attributes at all levels (top-level as well as nested objects)

Examples

Recursively remove foo attributes:

LET doc = { foo: { bar: { foo: 1, baz: 2 }, baz: 3 }, baz: 4 }
  RETURN UNSET_RECURSIVE(doc, "foo")
Show query results
Hide query results
[
  {
    "baz": 4
  }
]

Recursively remove bar attributes:

LET doc = { foo: { bar: { foo: 1, baz: 2 }, baz: 3 }, baz: 4 }
  RETURN UNSET_RECURSIVE(doc, "bar")
Show query results
Hide query results
[
  {
    "baz": 4,
    "foo": {
      "baz": 3
    }
  }
]

Recursively remove baz attributes:

LET doc = { foo: { bar: { foo: 1, baz: 2 }, baz: 3 }, baz: 4 }
  RETURN UNSET_RECURSIVE(doc, "baz")
Show query results
Hide query results
[
  {
    "foo": {
      "bar": {
        "foo": 1
      }
    }
  }
]

Recursively remove multiple attributes (foo and bar):

LET doc = { foo: { bar: { foo: 1, baz: 2 }, baz: 3 }, baz: 4 }
  RETURN UNSET_RECURSIVE(doc, "foo", "bar")
Show query results
Hide query results
[
  {
    "baz": 4
  }
]

Recursively remove multiple attributes (foo and baz), removing all attributes of the example object:

LET doc = { foo: { bar: { foo: 1, baz: 2 }, baz: 3 }, baz: 4 }
  RETURN UNSET_RECURSIVE(doc, "foo", "baz")
Show query results
Hide query results
[
  {}
]

Recursively remove multiple attributes (foo, bar, and baz), removing all attributes of the example object:

LET doc = { foo: { bar: { foo: 1, baz: 2 }, baz: 3 }, baz: 4 }
  RETURN UNSET_RECURSIVE(doc, "foo", "bar", "baz")
Show query results
Hide query results
[
  {}
]

UNSET_RECURSIVE(document, attributeNameArray) → doc

  • document (object): a document / object
  • attributeNameArray (array): an array of attribute names as strings
  • returns doc (object): document without the specified attributes at all levels (top-level as well as nested objects)

Examples

Recursively remove baz attributes, by passing an array with the attribute key:

LET doc = { foo: { bar: { foo: 1, baz: 2 }, baz: 3 }, baz: 4 }
  RETURN UNSET_RECURSIVE(doc, ["baz"])
Show query results
Hide query results
[
  {
    "foo": {
      "bar": {
        "foo": 1
      }
    }
  }
]

VALUES()

VALUES(document, removeInternal) → anyArray

Return the attribute values of the document as an array. Optionally omit system attributes.

  • document (object): a document / object
  • removeInternal (bool, optional): if set to true, then all internal attributes (such as _id, _key etc.) are removed from the result
  • returns anyArray (array): the values of document returned in any order

Examples

Get the attribute values of an object:

RETURN VALUES( { "_id": "users/jane", "name": "Jane", "age": 35 } )
Show query results
Hide query results
[
  [
    "users/jane",
    "Jane",
    35
  ]
]

Get the attribute values of an object, omitting system attributes:

RETURN VALUES( { "_id": "users/jane", "name": "Jane", "age": 35 }, true )
Show query results
Hide query results
[
  [
    "Jane",
    35
  ]
]

ZIP()

ZIP(keys, values) → doc

Return a document object assembled from the separate parameters keys and values.

keys and values must be arrays and have the same length.

  • keys (array): an array of strings, to be used as attribute names in the result
  • values (array): an array with elements of arbitrary types, to be used as attribute values
  • returns doc (object): a document with the keys and values assembled

Examples

RETURN ZIP( [ "name", "active", "hobbies" ], [ "some user", true, [ "swimming", "riding" ] ] )
Show query results
Hide query results
[
  {
    "name": "some user",
    "active": true,
    "hobbies": [
      "swimming",
      "riding"
    ]
  }
]