How to return vertices from Arango DB having one to many relationship in format like object and list of object for child vertices?

huangapple go评论87阅读模式
英文:

How to return vertices from Arango DB having one to many relationship in format like object and list of object for child vertices?

问题

以下是您提供的内容的翻译:

我在Arango DB中有Department和User作为顶点。 Department拥有许多用户。
Department是User的父节点。 Department与属于该部门的每个用户都有一条边。
我想以以下格式获取Department及其用户:

[
    {
        "department": {
            "id": 1011,
            "createdOn": 1682680058203,
            "graphLabel": "Department",
            "name": "Boston Department",
            "updatedOn": 1682680058203
        },
        "user": [
            {
                "emailId": "test@test.com",
                "firstName": "sam",
                "graphLabel": "User",
                "lastName": "test",
                "status": "ENABLED",
                "isActive": true,
                "isVerified": true,
                "createdOn": 1683875733291,
                "updatedOn": 1683875733291
            },
            {
                "emailId": "test@test2.com",
                "firstName": "kevin",
                "graphLabel": "User",
                "isActive": true,
                "isVerified": true,
                "lastName": "test",
                "status": "ENABLED",
                "updatedOn": 1682680059753
            }
        ]
    }
]

我正在使用以下查询获取所有部门及其所有用户:

for department in collection1 filter department.graphLabel=="Department"
for user in 1..2 any department graph 'collection1-graph' filter user.graphLabel=="User"
return {department,user}

上述查询返回以下响应:

[
    {
        "department": {
            "id": 1011,
            "createdOn": 1682680058203,
            "graphLabel": "Department",
            "name": "Boston Department",
            "updatedOn": 1682680058203
        },
        "user": {
            "emailId": "test@test.com",
            "firstName": "sam",
            "graphLabel": "User",
            "lastName": "test",
            "status": "ENABLED",
            "isActive": true,
            "isVerified": true,
            "createdOn": 1683875733291,
            "updatedOn": 1683875733291
        }
    },
    {
        "department": {
            "id": 1011,
            "createdOn": 1682680058203,
            "graphLabel": "Department",
            "name": "Boston Department",
            "updatedOn": 1682680058203
        },
        "user": {
            "emailId": "test@test2.com",
            "firstName": "kevin",
            "graphLabel": "User",
            "isActive": true,
            "isVerified": true,
            "lastName": "test",
            "status": "ENABLED",
            "updatedOn": 1682680059753
        }
    }
]
英文:

I have vertices as Department and User in Arango DB. Department is having many users.
Department is the parent of User. Department is having an edge with each user who belongs to a this department.
I want to fetch Department and its users in format as below:

[
    {
        "department": {
            "id": 1011,
            "createdOn": 1682680058203,
            "graphLabel": "Department",
            "name": "Boston Department",
            "updatedOn": 1682680058203
        },
        "user": [
            {
                "emailId": "test@test.com",
                "firstName": "sam",
                "graphLabel": "User",
                "lastName": "test",
                "status": "ENABLED",
                "isActive": true,
                "isVerified": true,
                "createdOn": 1683875733291,
                "updatedOn": 1683875733291
            },
            {
                "emailId": "test@test2.com",
                "firstName": "kevin",
                "graphLabel": "User",
                "isActive": true,
                "isVerified": true,
                "lastName": "test",
                "status": "ENABLED",
                "updatedOn": 1682680059753
            }
        ]
    }
]

I'm using below query to fetch all departments with its all users.

for department in collection1 filter department.graphLabel=="Department"
for user in 1..2 any department graph 'collection1-graph' filter user.graphLabel=="User"
return {department,user}

The above query returns response like:

[
    {
        "department": {
            "id": 1011,
            "createdOn": 1682680058203,
            "graphLabel": "Department",
            "name": "Boston Department",
            "updatedOn": 1682680058203
        },
        "user": {
            "emailId": "test@test.com",
            "firstName": "sam",
            "graphLabel": "User",
            "lastName": "test",
            "status": "ENABLED",
            "isActive": true,
            "isVerified": true,
            "createdOn": 1683875733291,
            "updatedOn": 1683875733291
        }
    },
    {
        "department": {
            "id": 1011,
            "createdOn": 1682680058203,
            "graphLabel": "Department",
            "name": "Boston Department",
            "updatedOn": 1682680058203
        },
        "user": {
            "emailId": "test@test2.com",
            "firstName": "kevin",
            "graphLabel": "User",
            "isActive": true,
            "isVerified": true,
            "lastName": "test",
            "status": "ENABLED",
            "updatedOn": 1682680059753
        }
    }
]

答案1

得分: 0

我通过使用LET子句找到了解决方案。

for department in collection1 filter department.graphLabel=="Department"
let user=(for user in 1..2 any department graph 'collection1-graph' filter user.graphLabel=="User" return user)
return {department,user}

LET子句将子查询的结果分配给变量user。最终结果以包含部门和相关用户数组的对象形式返回。

查询将返回一个包含部门及其相关用户的对象数组。

英文:

I was able to find the solution by using LET clause.

for department in collection1 filter department.graphLabel=="Department"
let user=(for user in 1..2 any department graph 'collection1-graph' filter user.graphLabel=="User" return user)
return {department,user}

The LET clause assigns the result of the subquery to the variable user. The final result is returned as an object containing the department and an array of associated users.

The query will return an array of objects, each containing the department and its associated users.

huangapple
  • 本文由 发表于 2023年5月17日 15:45:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269668.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定