Mongoose: how to define a combination of fields to be unique?

后端 未结 6 1897
半阙折子戏
半阙折子戏 2020-11-27 04:35

If I had a schema like this:

var person = new Schema({
  firstName:  String,
  lastName: String,
});

6条回答
  •  南笙
    南笙 (楼主)
    2020-11-27 04:58

    const personSchema = new Schema({ firstName:  String, lastName: String });
    const person = mongoose.model('recipients', personSchema);
    person.createIndexes();
    

    You might need to get rid of all duplicates in the collection or do it the faster and easy way :

    Edit your Code, Drop the Collection and then restart Mongo.

提交回复
热议问题