Thursday 2 September 2021

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

No comments:

Post a Comment