Khushi
Khushi

Reputation: 1849

how to fetch data from two collection in arangodb

my project backend is arangodb. I have two collections named "test" and "demo". i need to fetch data from both these tables. my data is like this:

test

[
  {
    "firstName": "abc",
    "lastName": "pqr",
    "company": "abc Industries",
    "id": "1234"
  },
  {
    "firstName": "xyz",
    "lastName": "qwe",
    "company": "xyz Industries",
    "id": "5678"
  }
] 

demo

[
  {
    "clientId": "1234",
    "subject": "test",
    "message": "testing",
    "priority": "High",
    "status": "closed",
    "id": "111111"
  },
  {
    "clientId": "1234", 
    "subject": "hiii",
    "message": "demo",
    "priority": "High",
    "status": "closed",
    "id": "222222"
  },
]

in this id of the test is same as clientid of the demo. i need to select data from the table that is data of the client "1234". how can i implement this using AQL(arango query language). i am new to arango. any suggestion will highly appreciable.

Upvotes: 0

Views: 1522

Answers (3)

udarajag
udarajag

Reputation: 23

For t in test
for d in demo 
filter t.id == d.clientId
filter t.id == @client
return {t,d}

Upvotes: 0

Thức Nguyễn
Thức Nguyễn

Reputation: 1

FOR collection IN [test,demo]
FOR x IN collection
RETURN x

Upvotes: -1

mpv89
mpv89

Reputation: 1891

You can do this with joins or subqueries.

A solution with a subqueries would look like:

FOR t IN test
  FILTER t.id == @client
  RETURN { 
    test: t,
    demo: (FOR d IN demo
             FILTER d.clientId == @client
             RETURN d)
  }

The @client is a bind parameter which contains your value 1234.

The result is:

[
  {
    "test": {
      "_key": "140306",
      "_id": "test/140306",
      "_rev": "_Urbgapq---",
      "company": "abc Industries",
      "firstName": "abc",
      "id": "1234",
      "lastName": "pqr"
    },
    "demo": [
      {
        "_key": "140233",
        "_id": "demo/140233",
        "_rev": "_UrbfyAm---",
        "clientId": "1234",
        "id": "222222",
        "message": "demo",
        "priority": "High",
        "status": "closed",
        "subject": "hiii"
      },
      {
        "_key": "140200",
        "_id": "demo/140200",
        "_rev": "_UrbfjfG---",
        "clientId": "1234",
        "id": "111111",
        "message": "testing",
        "priority": "High",
        "status": "closed",
        "subject": "test"
      }
    ]
  }
]

Upvotes: 1

Related Questions