I am working on a fairly big sveltekit project and use MongoDB for NodeJs. I have a serverless Atlas project. I try to get user data with the related user group data and with the (user group) related access data:
Example data:
users collection:
_id: ObjectId('641c689e0974d4617351897f'),
name: 'Bob',
email: 'bob@email.com',
user_group_ids: [
0: 630ccd1a3a2da7d625cc7ccf
]
user_groups collection:
_id: ObjectId('630ccd1a3a2da7d625cc7ccf'),
name: 'Editor',
access: [
0: {
accessTargetId: 6443fc97748be7952dab922b
}
]
access_tragets collection:
_id: ObjectId('6443fc97748be7952dab922b'),
name: 'posts',
url: '/posts'
This is my aggregation:
db.users.aggregate([
{
$match: {
_id: ObjectId.createFromHexString('630ccd943a2da7d625cc7cd4'),
},
},
{
$lookup: {
from: 'user_groups',
localField: 'user_group_ids',
foreignField: '_id',
let: { userGroupId: '$access' },
pipeline: [
{
$lookup: {
from: 'access_targets',
localField: 'userGroupId.accessTargetId',
foreignField: '_id',
as: 'access_targets_out',
},
},
],
as: 'user_groups',
},
}
]);
And I get this:
[
{
"name": "Bob",
"email": "bob@email.com",
"user_group_ids": [
{
"$oid": "630ccd1a3a2da7d625cc7ccf"
}
],
"user_groups": [
{
"_id": {
"$oid": "630ccd1a3a2da7d625cc7ccf"
},
"name": "Editor",
"access": [
{
"accessTargetId": {
"$oid": "6443fc84748be7952dab922a"
},
"accessRightId": {
"$oid": "644635c08de56feeeb600baa"
}
}
],
"access_targets_out": []
}
]
}
]
Why is the ‘access_targets_out’ array empty?
The lookup and pipeline documentation states that nested lookups in pipelines are possible.
If I use
localField: ‘$$userGroupId.accessTargetId’,
which should be correct accoding to the documentation, I get an error message:
FieldPath field names may not start with ‘$’. Consider using $getField or $setField.
Any help is pretty much appreciated.