Keen Jin
Keen Jin

Reputation: 1138

How to set the unique property for Schema by using mongoose?

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

Answers (1)

Michel Vorwieger
Michel Vorwieger

Reputation: 722

you just have to add unique: true e.g:

const mySchema = new Schema({
    test: {
        type: String,
        unique: true
    }
});

Upvotes: 3

Related Questions