ArangoDB 如何从Arango数据库中返回具有一对多关系的顶点,格式类似于对象和子顶点的对象列表?

vecaoik1  于 2023-05-21  发布在  Go
关注(0)|答案(1)|浏览(247)

我在Arango DB中将顶点设置为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
            },
            {
                "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
        }
    }
]
e4yzc0pl

e4yzc0pl1#

我能够通过使用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。最终结果作为包含部门和关联用户数组的对象返回。
查询将返回一个对象数组,每个对象都包含部门及其关联的用户。

相关问题