Reputation: 702
I'm designing a database on MongoDB for a website, where there are many events.
Each event has its basic details like, title
, description
, reviews
, etc.
Along with that, each event has different packages and booking price for each day. So a User
can book an Event
for a particular day for a particular package.
I have many solutions in my mind, but not sure which one to go for. At first, I came up with the following [Option 1]:
Event.schema({
title: String,
description: String,
reviews: [ { userId: mongoose.Types.ObjectId, comment: String, rating: Number } ],
prices: [ { packageName: String, date: Date, price: Number } ]
});
But there is a problem. If I want to change the package name, I have to do it in all the array elements.
I tried to make it better by this:
Event.schema({
title: String,
description: String,
reviews: [ { userId: mongoose.Types.ObjectId, comment: String, rating: Number } ],
packages: [ String ],
prices: [ { packageId: Number, date: Date, price: Number } ]
});
Here, the packageId
would be the position of the particular package name in packages
array. This also doesn't seem right. I don't know why. [Let me know if this is a good option]
The 3rd option that I could think of is to keep pricing for each package in a separate document:
Event.schema({
title: String,
description: String,
reviews: [ { userId: mongoose.Types.ObjectId, comment: String, rating: Number } ]
});
Package.schema({
eventId: { type: mongoose.Types.ObjectId, ref: 'Event' },
title: String,
pricing: [ { date: Date, price: Number } ]
});
Or instead of keeping eventId
in Package
model, should I keep an array of Package
IDs in Event
model?
Of all the options, which one is the best solution? Or how can it be improved, keeping in mind that I'll also be adding a Search by price
filter feature on my website. So I have to be very careful in deciding the schema right now.
Upvotes: 0
Views: 152
Reputation: 467
I think your first option is good, unless you want to change the package name often (which might not be the case).
The main reason this is the best option is because you want to search events by price. Having all the information in a single document will be more efficient.
You can find more in the MongoDB documentation
Upvotes: 1