Saturday 18 September 2021

ArangoDB: AQL: select specific properties of a document

Syntax1

FOR doc IN collection_name
    RETURN doc.property_name

Example

Get all the first names of users.

FOR doc IN users RETURN doc.firstName

127.0.0.1:8529@abc_org> queryToExecute = `FOR doc IN users RETURN doc.firstName`
FOR doc IN users RETURN doc.firstName

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

[ 
  "Madhu", 
  "Gopi", 
  "Ram", 
  "Siva", 
  "Joel", 
  "Jessi" 
]


Syntax2: Extract multiple properties

FOR doc IN collection_name
    FILTER condition
    RETURN {prop1 = doc.propName1, prop2 = doc.propName2..}


Example

FOR doc IN users RETURN {firstName: doc.firstName, lastName: doc.lastName}


127.0.0.1:8529@abc_org> queryToExecute = `FOR doc IN users RETURN {firstName: doc.firstName, lastName: doc.lastName}`
FOR doc IN users RETURN {firstName: doc.firstName, lastName: doc.lastName}

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

[ 
  { 
    "firstName" : "Madhu", 
    "lastName" : "Thatha" 
  }, 
  { 
    "firstName" : "Gopi", 
    "lastName" : null 
  }, 
  { 
    "firstName" : "Ram", 
    "lastName" : "Gurram" 
  }, 
  { 
    "firstName" : "Siva", 
    "lastName" : "Ponnam" 
  }, 
  { 
    "firstName" : "Joel", 
    "lastName" : "Chelli" 
  }, 
  { 
    "firstName" : "Jessi", 
    "lastName" : "Chelli" 
  } 
]


You can apply this projection with Filter operation also.

`FOR doc IN users
    FILTER doc.age > 35
   RETURN {firstName: doc.firstName, lastName: doc.lastName}`

127.0.0.1:8529@abc_org> queryToExecute = `FOR doc IN users
...>     FILTER doc.age > 35
...>    RETURN {firstName: doc.firstName, lastName: doc.lastName}`
FOR doc IN users
    FILTER doc.age > 35
   RETURN {firstName: doc.firstName, lastName: doc.lastName}

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

[ 
  { 
    "firstName" : "Jessi", 
    "lastName" : "Chelli" 
  } 
]


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment