I have a EventSchema that contains a field (seats) that is of type EventSeatSchema:
const EventSchema = new Schema<IEvent>({
name: { type: String, required: true },
description: { type: String, required: true },
location: { type: mongoose.Schema.Types.ObjectId, ref: 'Location', required: true },
performers: { type: [mongoose.Schema.Types.ObjectId], ref: 'Performer', required: true },
eventDate: { type: Date, required: true },
pictures: { type: [String], required: false },
seats: { type: [EventSeatSchema], required: true }
});
export const EventSeatSchema = new Schema<IEventSeat>({
seat: { type: mongoose.Schema.Types.ObjectId, ref: '???', required: true },
price: { type: Number, required: true },
available: { type: Boolean, required: true },
orderHistory: { type: [mongoose.Schema.Types.ObjectId], ref: 'Order', required: false }
});
The ‘seat’ field in EventSeatSchema should reference a document in the LocationSchema:
const LocationSchema = new Schema<ILocation>({
name: { type: String, unique: true, required: true },
country: { type: String, required: true },
province: { type: String, required: true },
address: { type: String, required: true },
pictures: { type: [String], required: true },
locationType: { type: mongoose.Schema.Types.ObjectId, ref: 'LocationType', required: true },
seatType: { type: mongoose.Schema.Types.ObjectId, ref: 'SeatType', required: true },
seats: { type: [FixedSeatSchema, AreaSeatSchema] },
seatsLayout: { type: String, required: true }
});
And here’s the problem: while Event and Location are actual collections in my db, SeatSchema (which is the Schema FixedSeatSchema and AreaSeatSchema are derived from) is just a schema with no model, so I can’t actually put a direct reference in the ref of seat for EventSeatSchema.
I wanted the reference basically to be able to use populate in queries.
So my question is if this is the right way to do things in MongoDB, on a SQL database I would have just created a separate table (collection) for all the seats, and I would have given the reference id to the other collections that needed it.
However it is my understanding that with MongoDB you should group the data that is related in the same documents as much as possible, so I tried this approach instead.
Is there a way to make this work? Or I should just move the Seat to a separate model and collection?