I'm trying to translate this aggregation to one within lookup but not successful
db.users.aggregate([
{ $match: { "medicalPractice.consultation.slots": { $exists: true } } },
{
"$addFields": {
"slots": {
$reduce: {
input: '$medicalPractice.consultation',
initialValue: [],
in: {
$concatArrays: [
"$$value",
{
$map: {
input: '$$this.slots',
as: "slot",
in: {
slot: "$$slot"
}
}
}]
}
}
}
}
},
{
"$addFields": {
slotsLength: {
$size: "$slots"
}
}
}
])
Here is what I'm trying:
db.profiles.aggregate([
{
$lookup: {
from: 'users',
localField: 'user',
foreignField: '_id',
as: 'user',
let: { consultation: "$medicalPractice.consultation" },
pipeline: [
{
"$addFields": {
"slots": {
$reduce: {
input: '$$consultation',
initialValue: [],
in: {
$concatArrays: [
"$$$value",
{
$map: {
input: '$$$this.slots',
as: "slot",
in: {
slot: "$$$slot"
}
}
}]
}
}
}
}
},
{
"$addFields": {
slotsLength: {
$size: "$$$slots"
}
}
}
]
},
},
{
$limit: 10
}
])
But for some reason still get this error "$lookup with 'pipeline' may not specify 'localField' or 'foreignField'"