I am working through a small example of my own based on the M121 course.
I have a simple Appointments
and Events
collection and would like join and return all documents that are associated with a specific provider
.
Appointment collection:
{
_id: ObjectId('62fd3e631d732145ed86adff'),
imported: false,
provider: ObjectId('62b62342654ae12263478489'),
status: "SCHEDULED",
summary: "Test appointment with client",
description: "This is a test description for the new booking type",
location: "4211 N Winfield Scott Plaza, Scottsdale, AZ 85251, USA",
startTime: 2022-08-17T04:30:00.000+00:00,
endTime: 2022-08-17T04:45:00.000+00:00,
createdAt: 2022-08-17T19:15:47.903+00:00,
updatedAt: 2022-08-17T19:15:47.903+00:00,
type:"external"
}
Event collection:
[
{
_id: ObjectId('62feaa0dca4ba048fa24dd45'),
summary: "Shampoo with Sandra Rodriguez",
description: "Please make sure to arrive at least 20 minutes before the...",
location: "4211 N Winfield Scott Plaza, Scottsdale, AZ 85251, USA",
creator: ObjectId('62b62342654ae12263478489'),
startTime: 2022-08-18T17:00:00.000+00:00,
endTime: 2022-08-18T17:15:00.000+00:00,
type: "external"
},
{
id: ObjectId('62fe983aca4ba048fa24dd43'),
summary: "Haircut with Derrick Jones",
description: "Meet with client 15 minutes after last scheduled appointment",
location: "4211 N Winfield Scott Plaza, Scottsdale, AZ 85251, USA",
creator: ObjectId('62b62342654ae12263478489'),
startTime: 2022-08-18T15:00:00.000+00:00,
endTime: 2022-08-18T15:30:00.000+00:00,
type: "external"
}
]
Below I have provided my aggregation but it’s only returning 1 document from Appointment
collection and not 3 documents (1 appointment, and 2 event docs).
Is this the correct way to approach this problem? Thank you for any suggestions.
[{
$match: {
provider: ObjectId('62b62342654ae12263478489')
}
}, {
$lookup: {
from: 'event',
localField: 'provider',
foreignField: 'creator',
as: 'Bookings'
}
}]