Hi!,
I am having trouble building a query and I am hoping to see if anybody can guide me on this.
I have the following documents in my bucket:
Project 1
"id": "123@project",
"_class": "com.package.Project",
"name": "project1",
"roles": [
{
"id": "abc@role",
"name": "Manager",
"projectId": "123@project"
},
{
"id": "def@role",
"name": "Developer",
"projectId": "123@project"
}
],
"status": "ACTIVE"
}
Project 2:
{
"id": "456@project",
"_class": "com.package.Project",
"name": "project2",
"roles": [
{
"id": "ghi@role",
"name": "Manager",
"projectId": "456@project"
},
{
"id": "jkl@role",
"name": "Developer",
"projectId": "456@project"
}
],
"status": "ACTIVE"
}
User 1:
{
"id": "123@user",
"_class": "com.package.User",
"name": "user1",
"projects": [
{
"projectId": "123@project"
}
],
"roles": [
"abc@role",
"def@role"
]
}
User 2:
{
"id": "456@user
"_class": "com.package.User",
"name": "user2",
"projects": [
{
"projectId": "123@project"
},
{
"projectId": "456@project"
}
],
"roles": [
"jkl@role"
]
}
Given these documents I need to get a list of projects but each result should include the list of users that have the “manager” role in that project.
The output should look like:
Result:
[
{
"id": "123@project",
"name": "project1",
"managers": [{
"id": "123@user",
"name": "user1"
}]
},
{
"id": "456@project",
"name": "project2",
"managers": []
}
]
I tried to build this query but got stuck when trying to select the managers for each project:
SELECT meta(pg).id id, pg.name name,
(
SELECT META(um).id
FROM identity um
WHERE um._class= "com.package.User"
AND ARRAY_CONTAINS(um.roles, <how-to-get-role-id-from-project-where-role-name-is-manager???>)
) AS managers
FROM identity p
JOIN identity u ON ANY x IN u.projects SATISFIES x.projectId = META(p).id END
WHERE p._class LIKE "com.package.Project" AND u._class LIKE "com.package.User"
GROUP BY p pg
The problem I am having is to select the role id from the project coming from the join. I tried different things like:
(
SELECT META(um).id, um.*
FROM identity um USE KEYS SELF.p.roles[*].id
WHERE um._class= "com.package.User"
AND ARRAY_CONTAINS(um.roles, (
SELECT META(r).id rid
FROM identity r
WHERE r._class = "com.package.Project"
AND ANY x IN r.roles SATISFIES x.name = "Manager"
AND x.projectId = META(SELF.p).id END) ) ) AS managers
but this didnt work.
Any advice on how to achieve this?
Thanks!
Mauro.-