I tried to create index from two fields in mongoose Schema but it didn't work this two fields are the id of two other schema and i want to be unique i get the ids from "mongoose.Schema.ObjectId"
this is my code:
const reviewSchema = new mongoose.Schema(
{
review: {
type: String,
required: [ true, 'Review can not be empty!' ],
},
rating: {
type: Number,
min: 1,
max: 5,
},
createdAt: {
type: Date,
default: Date.now,
},
tour: {
type: mongoose.Schema.ObjectId,
ref: 'Tour',
required: [ true, 'Review must belong to a tour.' ],
},
user: {
type: mongoose.Schema.ObjectId,
ref: 'User',
required: [ true, 'Review must belong to a user' ],
},
},
{
toJSON: { virtuals: true },
toObject: { virtuals: true },
},
);
reviewSchema.index({ tour: 1, user: 1 }, { unique: true });