Shikha thakur
Shikha thakur

Reputation: 1289

restrict to store duplicate values in mongodb

I am using mongodb for my application and i am facing a problem like a field is accepting duplicate values as well which i dont want

I want to know how to restrict it
I have followed an approach by specifying unique :true for a field quesListName

var mongoose = require('mongoose'),
Schema = mongoose.Schema;

var QuestionListSchema = new Schema({
    topicName: String,
    quesList: {
        quesListName: {
            type: String,
            unique: true
        },
        by: String
    }
});

but still it is accepting the duplicate value

Any help is highly appreciated

Upvotes: 0

Views: 365

Answers (1)

Amiram Korach
Amiram Korach

Reputation: 13296

You need to define the unique for the index:

var QuestionListSchema = new Schema({
    topicName: String,
    quesList: {
        quesListName: {
            type: String,
            index: {
              unique: true
            }
        },
        by: String
    }
});

http://mongoosejs.com/docs/api.html#schematype_SchemaType-index

Upvotes: 1

Related Questions