0

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

Shikha thakur
  • 1,269
  • 13
  • 34

1 Answers1

1

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

Amiram Korach
  • 13,056
  • 3
  • 28
  • 30
  • Thanks this worked for me With this i did another mistake i.e; i havent created index for that field btw it is fixed now :) – Shikha thakur Aug 04 '16 at 08:47