2017-04-19 60 views
0

考虑数据库具有以下独特记录,创建一个猫鼬的模式,其值是跨值

{id: 1, device: 'A'}, 
{id: 1, device: 'B'} 

现在,如果我要插入{id: 1, device: 'A'}记录不应该被创建为id: 1已经存在设备A

那么,Mongoose模式将如何显示?

回答

1

您可以为此类目的定义复合索引。

你的架构将是这样的:

const DeviceSchema = new Schema({ 
    id: { type: Number }, 
    device: { type: String }, 
}) 

DeviceSchema.index({ id: 1, device: 1}, { unique: true }); 

欲了解更多信息,请访问this link