Friday 17 September 2021

ArangoDB: FILTER the documents

FILTER operation is used to filter the documents.

 

Syntax

FOR doc IN collection_name
    FILTER condition
    RETURN doc

 

Let’s experiment with following users collection.

127.0.0.1:8529@abc_org> db.users.toArray()
[ 
  { 
    "_key" : "41688", 
    "_id" : "users/41688", 
    "_rev" : "_cSA6tje--A", 
    "id" : 3, 
    "firstName" : "Madhu", 
    "lastName" : "Thatha" 
  }, 
  { 
    "_key" : "41308", 
    "_id" : "users/41308", 
    "_rev" : "_cSBTpJC---", 
    "country" : "India", 
    "firstName" : "Gopi" 
  }, 
  { 
    "_key" : "43460", 
    "_id" : "users/43460", 
    "_rev" : "_cSBlsQe---", 
    "firstName" : "Ram", 
    "lastName" : "Gurram", 
    "age" : 35 
  }, 
  { 
    "_key" : "43468", 
    "_id" : "users/43468", 
    "_rev" : "_cSBl3BW---", 
    "firstName" : "Siva", 
    "lastName" : "Ponnam", 
    "age" : 35 
  }, 
  { 
    "_key" : "43482", 
    "_id" : "users/43482", 
    "_rev" : "_cSBmJqO---", 
    "firstName" : "Joel", 
    "lastName" : "Chelli", 
    "age" : 34 
  }, 
  { 
    "_key" : "43490", 
    "_id" : "users/43490", 
    "_rev" : "_cSBmXxW---", 
    "firstName" : "Jessi", 
    "lastName" : "Chelli", 
    "age" : 36 
  } 
]

Example 1: Find all users whose lastName is Chelli.

queryToExecute = `FOR doc IN users
    FILTER doc.lastName == "Chelli"
    RETURN doc`

127.0.0.1:8529@abc_org> queryToExecute = `FOR doc IN users
...>     FILTER doc.lastName == "Chelli"
...>     RETURN doc`
FOR doc IN users
    FILTER doc.lastName == "Chelli"
    RETURN doc

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

[ 
  { 
    "_key" : "43482", 
    "_id" : "users/43482", 
    "_rev" : "_cSBmJqO---", 
    "firstName" : "Joel", 
    "lastName" : "Chelli", 
    "age" : 34 
  }, 
  { 
    "_key" : "43490", 
    "_id" : "users/43490", 
    "_rev" : "_cSBmXxW---", 
    "firstName" : "Jessi", 
    "lastName" : "Chelli", 
    "age" : 36 
  } 
]


Example 2: Get all the documents whose lastName is Chelli and firstName is Joel.

queryToExecute = `FOR doc IN users
    FILTER doc.firstName == "Joel" AND doc.lastName == "Chelli"
    RETURN doc`

127.0.0.1:8529@abc_org> queryToExecute = `FOR doc IN users
...>     FILTER doc.firstName == "Joel" AND doc.lastName == "Chelli"
...>     RETURN doc`
FOR doc IN users
    FILTER doc.firstName == "Joel" AND doc.lastName == "Chelli"
    RETURN doc

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

[ 
  { 
    "_key" : "43482", 
    "_id" : "users/43482", 
    "_rev" : "_cSBmJqO---", 
    "firstName" : "Joel", 
    "lastName" : "Chelli", 
    "age" : 34 
  } 
]


Example 3: Find all users whose lastName is Chelli or Gurram.

queryToExecute = `FOR doc IN users
    FILTER doc.lastName == "Gurram" OR doc.lastName == "Chelli"
    RETURN doc`

127.0.0.1:8529@abc_org> queryToExecute = `FOR doc IN users
...>     FILTER doc.lastName == "Gurram" OR doc.lastName == "Chelli"
...>     RETURN doc`
FOR doc IN users
    FILTER doc.lastName == "Gurram" OR doc.lastName == "Chelli"
    RETURN doc

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

[ 
  { 
    "_key" : "43460", 
    "_id" : "users/43460", 
    "_rev" : "_cSBlsQe---", 
    "firstName" : "Ram", 
    "lastName" : "Gurram", 
    "age" : 35 
  }, 
  { 
    "_key" : "43482", 
    "_id" : "users/43482", 
    "_rev" : "_cSBmJqO---", 
    "firstName" : "Joel", 
    "lastName" : "Chelli", 
    "age" : 34 
  }, 
  { 
    "_key" : "43490", 
    "_id" : "users/43490", 
    "_rev" : "_cSBmXxW---", 
    "firstName" : "Jessi", 
    "lastName" : "Chelli", 
    "age" : 36 
  } 
]


Example 4: Find all users whose age is > 35.

queryToExecute = `FOR doc IN users
    FILTER doc.age > 35
    RETURN doc`

127.0.0.1:8529@abc_org> queryToExecute = `FOR doc IN users
...>     FILTER doc.age > 35
...>     RETURN doc`
FOR doc IN users
    FILTER doc.age > 35
    RETURN doc

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

[ 
  { 
    "_key" : "43490", 
    "_id" : "users/43490", 
    "_rev" : "_cSBmXxW---", 
    "firstName" : "Jessi", 
    "lastName" : "Chelli", 
    "age" : 36 
  } 
]


  

Previous                                                    Next                                                    Home

No comments:

Post a Comment