【发布时间】:2015-09-07 21:08:28
【问题描述】:
目标
为两列创建唯一性
我尝试了什么
这是我的架构,
var mongoose = require('mongoose');
// location table Schema
var locationSchema = new mongoose.Schema({
locationId: { type: String, required: true },
stockingLocationId: { type: String, required: true},
parentStockingLocationId: { type: String },
stockingLocationDescription: { type: String },
created: { type: Date, default: Date.now },
lastModified: { type: Date, default: Date.now },
isActive: { type: Boolean , default : true },
isDeleted: { type: Boolean , default : false }
});
两列是locationId 和stockingLocationId。
我试过locationSchema.index({locationId:1, stockingLocationId:1}, { unique: true });
但不起作用,
有什么帮助吗?
【问题讨论】:
-
你能告诉我为什么是负面的吗?
-
@ramamoorthy-villi 代码是正确的。一旦删除数据库并重新启动服务器。然后检查它是否工作!
-
@Vishnu,谢谢,在我添加 index:true 后,它正在工作,例如 locationId: { type: String, required: true,index:true }, stockingLocationId: { type: String, required: true,index :true} 在架构中