Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
285 views
Welcome To Ask or Share your Answers For Others

1 Answer

I found it was a bug from atlas ,I don't know why but it couldnt create index with options I create a local data base and now it works


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...