2016-08-04 50 views
0

我使用MongoDB的我的申請,我面臨着像一個領域的問題接受重複的值,以及我不想限制重複的值存儲在MongoDB中

我想知道如何限制它
我已經通過指定unique :true一個字段quesListName

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

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

但它畢竟是接受重複值

任何幫助,隨後的方法是高度更加感激ated

+0

嘗試重新啓動蒙戈 – chirag

+0

你'quesList'不是一個名單,但只是一個對象。你真的得到了什麼樣的重複?你能舉個例子嗎? – str

+0

或參考此鏈接:http://stackoverflow.com/questions/27354834/mongoose-unique-true-not-work – chirag

回答

1

您需要定義唯一的指標:

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

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

+0

感謝這工作對我 與此我做了另一個錯誤,即;我還沒有創建該字段的索引 btw它現在是固定的:) –