Thursday, 2 September 2021

AQL using ArangoDB web UI

In upcoming posts, I am going to explain how to execute AQL queries using Arango Web UI.

 

Let’s create a collection ‘users’ using ArangoDB web ui.

 

Step 1: Login to ArangoDB web user interface.

 


Click on Add Collection widget.

 


 

Give the collection name as users and click on Save button.

 

 


If everything goes well, users collection will be created successfully.



 

Previous                                                    Next                                                    Home

ArangoDB: AQL: Insert multiple documents

  Below snippet is used to insert multiple documents to ArangoDB person collection.

dataToInsert = [
  {"id" : 124, "firstName" : "Janani"},
  {"id": 125, "firstName" : "Prathap", "age" : 35},
  {"id": 126, "firstName": "Saurav", "lastName" : "Sarkar", "age": 41}
]

for(docIndex in dataToInsert){
  queryToExecute = "INSERT " + JSON.stringify(dataToInsert[docIndex]) + " INTO person"
  db._query(queryToExecute)
}

 

Arangosh execution code

127.0.0.1:8529@abc_org> db._create("person")
[ArangoCollection 40320, "person" (type document, status loaded)]

127.0.0.1:8529@abc_org> dataToInsert = [
...>   {"id" : 124, "firstName" : "Janani"},
...>   {"id": 125, "firstName" : "Prathap", "age" : 35},
...>   {"id": 126, "firstName": "Saurav", "lastName" : "Sarkar", "age": 41}
...> ]
[ 
  { 
    "id" : 124, 
    "firstName" : "Janani" 
  }, 
  { 
    "id" : 125, 
    "firstName" : "Prathap", 
    "age" : 35 
  }, 
  { 
    "id" : 126, 
    "firstName" : "Saurav", 
    "lastName" : "Sarkar", 
    "age" : 41 
  } 
]

127.0.0.1:8529@abc_org> for(docIndex in dataToInsert){
...>   queryToExecute = "INSERT " + JSON.stringify(dataToInsert[docIndex]) + " INTO person"
...>   db._query(queryToExecute)
...> }
[object ArangoQueryCursor, count: 0, cached: false, hasMore: false]

 

Query person collection and retrieve all the data

127.0.0.1:8529@abc_org> db.person.toArray()
[ 
  { 
    "_key" : "40338", 
    "_id" : "person/40338", 
    "_rev" : "_cSAdFWq---", 
    "id" : 124, 
    "firstName" : "Janani" 
  }, 
  { 
    "_key" : "40341", 
    "_id" : "person/40341", 
    "_rev" : "_cSAdFWu---", 
    "id" : 125, 
    "firstName" : "Prathap", 
    "age" : 35 
  }, 
  { 
    "_key" : "40344", 
    "_id" : "person/40344", 
    "_rev" : "_cSAdFWy---", 
    "id" : 126, 
    "firstName" : "Saurav", 
    "lastName" : "Sarkar", 
    "age" : 41 
  } 
]

 

 

 

 

 

Previous                                                    Next                                                    Home

ArangoDB: AQL: Insert a document

Syntax

INSERT document INTO collectionName;

 

Example

var queryToExecute = `INSERT {
    "id": 123,
    "firstName" : "Krishna",
    "lastName" : "Gurram",
    "age": 33
} INTO person`

db._query(query)

127.0.0.1:8529@abc_org> var queryToExecute = `INSERT {
...>     "id": 123,
...>     "firstName" : "Krishna",
...>     "lastName" : "Gurram",
...>     "age": 33
...> } INTO person`

127.0.0.1:8529@abc_org> db._query(queryToExecute)
[object ArangoQueryCursor, count: 0, cached: false, hasMore: false]

 

Let’s query person collection.

127.0.0.1:8529@abc_org> db.person.toArray()
[ 
  { 
    "_key" : "39652", 
    "_id" : "person/39652", 
    "_rev" : "_cSAMnsi---", 
    "id" : 123, 
    "firstName" : "Krishna", 
    "lastName" : "Gurram", 
    "age" : 33 
  } 
]

 



Previous                                                    Next                                                    Home

ArangoDB: AQL: FOR: iterate over a collection

FOR operation is used to iterate over a collection or view, array of elements.

 

Syntax

FOR variableName IN expression

 

Let’s experiment with given example.

127.0.0.1:8529@abc_org> db.employees.toArray()
[ 
  { 
    "_key" : "66087", 
    "_id" : "employees/66087", 
    "_rev" : "_cS6haCC---", 
    "id" : 1, 
    "firstName" : "Krishna", 
    "lastName" : "Gurram", 
    "age" : 34 
  }, 
  { 
    "_key" : "66101", 
    "_id" : "employees/66101", 
    "_rev" : "_cS6hpna---", 
    "id" : 2, 
    "firstName" : "Gopi", 
    "lastName" : "Battu", 
    "age" : 35 
  }, 
  { 
    "_key" : "66109", 
    "_id" : "employees/66109", 
    "_rev" : "_cS6h5Pe---", 
    "id" : 3, 
    "firstName" : "Deeraj", 
    "lastName" : "Arora", 
    "age" : 41 
  } 
]

 

Example: Iterate over employees collection.

Following query is used to iterate over an employees collection.

FOR e in  employees RETURN e

127.0.0.1:8529@abc_org> queryToExecute  = `FOR e in  employees RETURN e`
FOR e in  employees RETURN e

127.0.0.1:8529@abc_org> db._query(queryToExecute)
[object ArangoQueryCursor, count: 3, cached: false, hasMore: false]

[ 
  { 
    "_key" : "66087", 
    "_id" : "employees/66087", 
    "_rev" : "_cS6haCC---", 
    "id" : 1, 
    "firstName" : "Krishna", 
    "lastName" : "Gurram", 
    "age" : 34 
  }, 
  { 
    "_key" : "66101", 
    "_id" : "employees/66101", 
    "_rev" : "_cS6hpna---", 
    "id" : 2, 
    "firstName" : "Gopi", 
    "lastName" : "Battu", 
    "age" : 35 
  }, 
  { 
    "_key" : "66109", 
    "_id" : "employees/66109", 
    "_rev" : "_cS6h5Pe---", 
    "id" : 3, 
    "firstName" : "Deeraj", 
    "lastName" : "Arora", 
    "age" : 41 
  } 
]

 

 

 

Previous                                                    Next                                                    Home

ArangoDB: AQL: High level operations

Following table summarizes list of high level operations supported in AQL.

 

Operation

Description

FOR

User to iterate over a collection or view and traverse a graph.

RETURN

Return the result of the query

FILTER

Used to filter the data based on given conditions.

SEARCH

Full text search the index on an ArangoSearch View

SORT

Sort the results

LIMIT

Limit the number of elements in the result

LET

Define a variable with given value.

COLLECT

Group an array by one or multiple group criteria.

REMOVE

Remove documents from a collection

UPDATE

Partially update the documents in a collection.

REPLACE

Replace the documents in a collection

INSERT

Insert new documents into a collection

UPSERT

Update/replace an existing document, or create it in the case it does not exist.

WITH

Specify the collections used in a query

Previous                                                    Next                                                    Home

AQL: Exploring ArangoDB Query language

ArangoDB query language is used to perform CRUD operations against the data stored in ArangoDB.

 

Is AQL support both DDL and DML?

Right now AQL support only DML (Data manipulation language). You can manipulate the data using AQL. But you can’t create and drop databases, collections and views using AQL.

 

How to execute AQL queries?

Following approaches are possible to execute AQL queries.

a.   Using Arango shell

b.   Using web interface

c.    Using raw http calls

In this tutorial series, I am going to use arangosh to experiment with AQL queries.

Previous                                                    Next                                                    Home

ArangoDB: Enforce schema at collection level

ArangoDB is schema less by default, But it allows you enforce schema at collection level.

 

How to define a schema?

Schema is defined in json schema format (https://json-schema.org/).

 

For example,

var schema = {
  "rule": {
    "properties": {
      "id": {
        "type": "integer"
      },
      "firstName": {
        "type": "string"
      },
      "lastName": {
        "type": "string"
      },
      "hobbies": {
        "type": "array",
        "items": {
          "type": "string"
        }
      }
    },
    "required": ["id", "firstName", "lastName"],
    "additionalProperties": false
  },
  "level": "moderate",
  "message": "Valid attributes are id, firstName, lastName and hobbies. id, firstName and lastName are mandatory attributes."

}

 

Above schema defines how the document in a collection should looks like. Schema define that a document has 4 attributes.

a.   id

b.   firstName

c.    lastName

d.   hobbies

 

In these 4 attributes id, firstName and lastName are mandatory and hobbies is optional. As per this schema document shouldn’t contain any additional attributes.

 

Level control

Level controls when the validation should be triggered. Below table summarizes possible values associated with level attribute.

 

Value

Description

none

Validation is turned off

new

Validation is applicable to only newly inserted documents.

moderate

New and modified documents must pass validation except for modified documents where the OLD value did not pass validation already.

strict

All new and modified document must strictly pass validation.

 

Error message

When the schema validation fails, ArangoDB throws an error. You can customize this error message using ‘ message’ attribute.

 

Create a collection using this schema

db._create("person", {"schema": schema})

127.0.0.1:8529@abc_org> var schema = {
...>   "rule": {
...>     "properties": {
...>       "id": {
...>         "type": "integer"
...>       },
...>       "firstName": {
...>         "type": "string"
...>       },
...>       "lastName": {
...>         "type": "string"
...>       },
...>       "hobbies": {
...>         "type": "array",
...>         "items": {
...>           "type": "string"
...>         }
...>       }
...>     },
...>     "required": ["id", "firstName", "lastName"],
...>     "additionalProperties": false
...>   },
...>   "level": "moderate",
...>   "message": "Valid attributes are id, firstName, lastName and hobbies. id, firstName and lastName are mandatory attributes."
...> 
...> }

127.0.0.1:8529@abc_org> db._create("person", {"schema": schema})
[ArangoCollection 37662, "person" (type document, status loaded)]

 

Try to insert documents which are not comply with the schema.

127.0.0.1:8529@abc_org> db.person.save({"id" : 1})
JavaScript exception in file '/usr/local/Cellar/arangodb/3.7.11_1/share/arangodb3/js/client/modules/@arangodb/arangosh.js' at 99,7: ArangoError 1620: Valid attributes are id, firstName, lastName and hobbies. id, firstName and lastName are mandatory attributes.
!      throw error;
!      ^
stacktrace: ArangoError: Valid attributes are id, firstName, lastName and hobbies. id, firstName and lastName are mandatory attributes.
    at Object.exports.checkRequestResult (/usr/local/Cellar/arangodb/3.7.11_1/share/arangodb3/js/client/modules/@arangodb/arangosh.js:97:21)
    at ArangoCollection.save.ArangoCollection.insert (/usr/local/Cellar/arangodb/3.7.11_1/share/arangodb3/js/client/modules/@arangodb/arango-collection.js:965:14)
    at <shell command>:1:11

127.0.0.1:8529@abc_org> db.person.save({"id" : 1, "firstName": "Krishna", "lastName": "Gurram", "a" : 1})
JavaScript exception in file '/usr/local/Cellar/arangodb/3.7.11_1/share/arangodb3/js/client/modules/@arangodb/arangosh.js' at 99,7: ArangoError 1620: Valid attributes are id, firstName, lastName and hobbies. id, firstName and lastName are mandatory attributes.
!      throw error;
!      ^
stacktrace: ArangoError: Valid attributes are id, firstName, lastName and hobbies. id, firstName and lastName are mandatory attributes.
    at Object.exports.checkRequestResult (/usr/local/Cellar/arangodb/3.7.11_1/share/arangodb3/js/client/modules/@arangodb/arangosh.js:97:21)
    at ArangoCollection.save.ArangoCollection.insert (/usr/local/Cellar/arangodb/3.7.11_1/share/arangodb3/js/client/modules/@arangodb/arango-collection.js:965:14)
    at <shell command>:1:11

 

Let’s insert a document that is comply with the schema.

127.0.0.1:8529@abc_org> db.person.save({"id" : 1, "firstName": "Krishna", "lastName": "Gurram"})
{ 
  "_id" : "person/37705", 
  "_key" : "37705", 
  "_rev" : "_cS_cpIC---" 
}

 


 

Previous                                                    Next                                                    Home