Reputation: 283
I am joining booking and customers collections. My bookings object looks like this:
{
"bookingID": 1,
"customerID": "[email protected]",
"room": [
{
"roomID": "JDS",
"numRooms": 1
}
]
}
and customer object like this:
{
"customer": {
"firstname": "Adam",
"surname": "Apple",
"title": "Nr.",
"emailAddress": "[email protected]"
},
"address": {
"street": "221B Baker St",
"city": "London",
"state": null,
"zipcode": "NW1 6XE",
"country": "UK"
}
}
I am using this code:
{ $lookup: {from: 'customers', localField: 'customerID', foreignField: 'customer.emailAddress', as: 'traveller'} },
{ $unwind: '$traveller' },
{ $project: {'traveller': {_id: 0, 'address': 0}} }
to combine the output. I'd like to get:
"traveller": {
"firstname": "Adam",
"surname": "Apple",
"title": "Nr.",
"emailAddress": "[email protected]"
}
and I get:
"traveller": {
"customer": {
"firstname": "Adam",
"surname": "Apple",
"title": "Nr.",
"emailAddress": "[email protected]"
}
}
How do I do this?
You can check it here: https://mongoplayground.net/p/ik2UtP4-EHa
Upvotes: 0
Views: 133
Reputation: 1041
Try this one its not break your other pipelines
aggregate([{
$lookup: {
from: 'customers',
localField: 'customerID',
foreignField: 'customer.emailAddress',
as: 'traveller'
} },
{ $unwind: '$traveller' },
{ $project: {
'traveller':"$traveller.customer",
bookingID:1,
cruise:1,
customerID:1,
room:1,
} }])
Upvotes: 1
Reputation: 1615
Here you go :-
Just change the project stage as below -
{
$project: {
"traveller": "$traveller.customer",
"bookingID": 1,
"cruise": 1,
"customerID": 1,
"room": 1
}
}
Upvotes: 1