多列独特的mongoose节点的组合

目的

为两列创build唯一性

我试过了

这是我的模式,

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 } }); 

两列是locationIdstockingLocationId

我尝试了locationSchema.index({locationId:1, stockingLocationId:1}, { unique: true });

但不行,

有什么帮助吗?

我知道这有点晚了,但沿着这些线路。

 mySchema.index({field1: 1, field2: 1}, {unique: true}); 

如此处所述

我认为你必须分别要求两栏。 你这样做,两列的组合将是唯一的(组合独特的关键)。

 locationSchema.index({locationId:1}, { unique: true }); locationSchema.index({stockingLocationId:1}, { unique: true }); 

在更改索引架构之后,您需要删除数据库