So basically what I'm trying to achieve is to get tickets from a concert. every ticket has a seller ID
which I want to join with the collection users.
This is the query I have, to get all the tickets from a concert:
let concerts = await Concert.aggregate([
{$lookup: { from: 'artists',localField:'artistId',foreignField: '_id',as: 'artist'}},
{$unwind: "$artist"},
{$lookup: { from: 'tickets', localField: '_id', foreignField: 'concertId' , as : 'tickets' }},
])
I have tried everything to join the seller ID inside the tickets with the user collection, but nothing has really worked out so far.
Here are my schemas:
Concert:
const ConcertSchema = new mongoose.Schema({
title: String,
date: Date,
address: String,
genre: String,
artistId: mongoose.Types.ObjectId,
}, {timestamps: true});
Ticket:
const TicketSchema = new mongoose.Schema({
type: String,
price: Number,
redeemed: Boolean,
redeemedAt: Date,
sellerId: mongoose.Types.ObjectId,
buyerId: mongoose.Types.ObjectId,
concertId: mongoose.Types.ObjectId,
}, {timestamps: true});
And User:
var UserSchema = new mongoose.Schema({
username: String,
password: String,
email: String,
walletId: mongoose.Types.ObjectId,
}, {timestamps: true});
This is what i want to achieve:
[ { "concert1": { "title": "this is the concerts title", "tickets": [ { "price": 12.99, "seller": { "username": "user1" } }, { "price": 12.99, "seller": { "username": "user1" } } ] } } ]