Uniqueness with multiple keys in the mongoose

If I want any field to be unique and avoid duplication, I use the syntax described below.

var schema = mongoose.Schema({
  projectName : String,
  authorName : { type: String, index: true }
});

      

But what if I want the (project_name, authorName) pairs to be unique. I know mongodb supports this with

db.collection.ensureIndex( { a: 1, b: 1 }, { unique: true } )

      

How to write the same in mongoose? What is the syntax for this.

+3


source to share


1 answer


Create a schema-level index as in the docs :



schema.index({projectName:1, authorName:1}, { unique: true });

      

+3


source







All Articles