3

If I want to have any field unique and avoid duplications I use the syntax described as follows

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

But what if I want to have the value of pairs of (projectName, authorName) unique. I know that mongodb supports this with

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

How do I write the same thing in mongoose? What is the syntax for doing it.

raju
  • 4,788
  • 15
  • 64
  • 119

1 Answers1

4

Create a schema level index, as in the docs:

schema.index({projectName:1, authorName:1}, { unique: true });
BatScream
  • 19,260
  • 4
  • 52
  • 68