限制在 mongodb 中存储重复值 [英] restrict to store duplicate values in mongodb
本文介绍了限制在 mongodb 中存储重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为我的应用程序使用 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
我想知道怎么限制
我遵循了一种方法,为字段 quesListName
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var QuestionListSchema = new Schema({
topicName: String,
quesList: {
quesListName: {
type: String,
unique: true
},
by: String
}
});
但它仍然接受重复值
非常感谢任何帮助
推荐答案
您需要为索引定义唯一性:
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
这篇关于限制在 mongodb 中存储重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文