I’ve got two collections: Organization
and User
. A typical organization document looks something like this:
type Organization = {
"_id": string,
// This looks a little moronic to have both an id and an orgId.
// This is out of necessity, as pipeline variables cannot be drawn
// from values that start with an _, as best as I understand it.
// When I try to start a variable with a _, Compass indicates that
// "<variable> starts with an invalid character for a user variable name"
"orgId": string,
}
A typical user looks something like this:
type Permissions = "orgAdmin"
type User = {
"_id": string,
"orgId": string,
"permissions": Permissions[],
}
I’m trying to build an aggregation that will do a left join between Organizations and Users, including all organization users that have orgAdmin
permissions. Here it is at the moment:
{
from: 'User',
let: {
"orgId": "$orgId",
"permissions": "$permissions"
},
as: 'orgAdmins',
pipeline: [
{
$match: {
$and: [
{
$expr: {
$eq: ["$orgId", "$$orgId"]
}
},
{
$expr: {
$in: ["orgAdmin", "$$permissions"]
}
}
]
}
},
]
}
Joining on orgId
is a no brainer. It works great. The trouble I’m running into is around the array value inclusion check. My understanding is that access to fields in joined documents requires defining variables using let
, and that accessing the variables in your pipeline is done with $$
(not the $
used in the variable’s declaration). However, when I try to evaluate the aggregation in Compass I get the following warning and no returned records:
Failed to optimize pipeline :: caused by :: Failed to optimize expression :: caused by :: Failed to optimize expression :: caused by :: $in requires an array as a second argument, found: missing
I’m totally stumped. I’m not sure how to pass the permissions
array to the $in
aggregation. Any help would be appreciated.
Thanks in advance,
Chris.