我遵循mongoose架构:
{
"_id" : ObjectId("5971b0a0d5602b15a8068ab7"),
"trainnum" : "011Ф",
"startplace" : "Tashkent",
"destination" : "Samarkand",
"updated_at" : ISODate("2017-07-21T07:43:28.620Z"),
"weeks" : [
"1",
"2",
"3",
"4",
"5",
"6",
"0"
],
"vagons" : [
{
"_id" : ObjectId("5971daead5602b15a8068abd"),
"numvag" : "01K",
"seatcount" : "40",
"reserves" : [
{
"numseats" : "1-10",
"numreserve" : "1",
"_id" : ObjectId("5971daead5602b15a8068abf")
},
{
"numseats" : "11-27",
"numreserve" : "2",
"_id" : ObjectId("5971daead5602b15a8068abe")
}
],
"date2" : ISODate("2017-07-21T00:00:00Z"),
"date1" : ISODate("2017-07-21T00:00:00Z")
}
],
"date2" : ISODate("2017-12-31T00:00:00Z"),
"date1" : ISODate("2017-01-01T00:00:00Z"),
"__v" : 0
}
我想在特定日期使numvag独一无二。我的意思是它不允许我为现有的date1和date2添加相同的numvag值。
答案 0 :(得分:0)
通过mongoose-unique-validator
安装npm install mongoose-unique-validator
(https://www.npmjs.com/package/mongoose-unique-validator)
到您的正常架构,
const myScheme = Mongoose.Schema({
email : { required: true }
})
要求模块并附加" uniqueValidator"到最后如下,
const uniqueValidator = require('mongoose-unique-validator');
const myScheme = Mongoose.Schema({
email : { required: true }
}).plugin(uniqueValidator);
现在,您可以添加除默认模式之外的其他模式
const uniqueValidator = require('mongoose-unique-validator');
const myScheme = Mongoose.Schema({
email : {
required: true,
unique: true
}
}).plugin(uniqueValidator);
因此,如果您尝试为电子邮件插入相同的记录,则会引发错误。