Hi Everyone!!
I have the following data
Functions collection:
[
{
_id: 1,
function: 'auth_group',
roles: [ 'superadmin', 'manager' ],
currentUser: { _id: 2, account: 'admin', roles: [ 'administrator', 'staff' ] }
},
{
_id: 3,
function: 'auth_user',
roles: [ 'administrator', 'manager' ],
currentUser: { _id: 4, account: 'admin', roles: [ 'administrator', 'staff' ] }
}
]
I want to search for functions where the currentUser has been assigned at least one role. The desired result is:
[
{
_id: 3,
function: 'auth_user',
roles: [ 'administrator', 'manager' ],
currentUser: { _id: 4, account: 'admin', roles: [ 'administrator', 'staff' ] }
}
]
I tried the following method, but the result was not as expected:
db.Functions.aggregate(
[
{
"$match": {
"$expr": {
"$in": [
"$roles",
"$currentUser.roles"
]
}
}
}
]
)
Thanks