Reputation: 1138
I use Mongoose in NodeJS to operate MongoDB database. One problem I have is that when I define a Schema, I want to set a field in that Schema to be unique. I set the field according to the instructions of the official document, but it didn't work. Some people know that when defining Schema, how to define a field value is unique? The Mongoose version I used is V5.2.13.
Upvotes: 1
Views: 582
Reputation: 722
you just have to add unique: true
e.g:
const mySchema = new Schema({
test: {
type: String,
unique: true
}
});
Upvotes: 3