多列唯一mongoose nodejs组合

ram*_*lli 2 mongoose mongodb node.js

客观的

为两列创建唯一性

我试过的

这是我的架构,

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 }

});
Run Code Online (Sandbox Code Playgroud)

两列是locationIdstockingLocationId

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

但不起作用,

请问有什么帮助吗?

Oci*_*rne 8

我知道这有点晚了,但沿着这些路线的东西..

mySchema.index({field1: 1, field2: 1}, {unique: true});
Run Code Online (Sandbox Code Playgroud)

在指定的位置


Ali*_*786 7

在像下面这样的字段周围添加单引号后,它对我有用

mySchema.index({'field1': 1, 'field2': 1}, {unique: true});
Run Code Online (Sandbox Code Playgroud)

  • 有两件事:这应该是官方答案,而且猫鼬开发人员应该被解雇。 (4认同)

小智 5

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