多列唯一猫鼬nodejs的组合 [英] Combination of multiple column unique mongoose nodejs

查看:65
本文介绍了多列唯一猫鼬nodejs的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标

为两列创建唯一性

我尝试过的

这是我的架构,

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

此处中指定

Objective

To create uniqueness for two columns

what i tried

Here is my schema,

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 }

});

Two columns are locationId and stockingLocationId.

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

but not works,

Any help Please?

解决方案

I know it's a little late but something along these lines..

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

as specified in here

这篇关于多列唯一猫鼬nodejs的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆