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

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

If I had a schema like this:

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

6条回答
  •  北海茫月
    2020-11-27 05:01

    I haven't tried this, but using a unique index should do the trick.

    db.person.ensureIndex( { "firstname": 1, "lastname": 1 }, { unique: true } )
    

提交回复
热议问题