如果 A 和 B 相同,则 mongodb 复合索引唯一 [英] mongodb compound index unique if A and B both are same

查看:47
本文介绍了如果 A 和 B 相同,则 mongodb 复合索引唯一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多类似的问题,但没有一个能解决我的问题:(

There are many similar questions but none of them solved my question :(

如何索引Mongoose 中单个文档中的多个唯一数据字段?

MongoDb 在字段上创建唯一索引并非在所有文档中

在我的收藏中组合两个独一无二的字段

我的项目架构在这里

{
    NamespaceId: {
        type: ObjectId,
        required: true
    },
    Pattern: {
        type: String,
        required: true,
        default: ''
    },
    Key: {
        type: String,
        required: true,
    },
    Value: {
        type: String,
        required: true
    },
    CreatedBy: {
        type: String,
        required: true
    },
    Comments: {
        type: String,
        required: false,
        default: ''
    },
    IsBanned: {
        type: Boolean,
        required: true,
        default: false
    },
    Status: {
        type: String,
        required: true
    }
}

现在,我的收藏中有以下文档

Now, I have following documents in my collections

{
    NamespaceId: 123,
    Key: 'first key',
    Value: 'first value',
    W: '1'
},
{
    NamespaceId: 456,
    Key: 'second key',
    Value: 'second value',
    W: 2
},
{
    NamespaceId: 789,
    Key: 'third key',
    Value: 'third value',
    W: '3'
}

我希望 NamespaceId Key Value 是唯一的.所以我为上述系列创建了一个复合独特的.

I want NamespaceId and Key and Value are unique. So I create a compounded unique for above collection.

ItemSchema.index({
    'Key': 1,
    'Value': 1,
    'NamespaceId': 1
},{unique: true })

根据我的想法,以下文档可以插入,因为集合中不存在相同的索引.

From my thoughts, following document can insert into because there no same indexes exist in collection.

{
    NamespaceId: 123,
    Key: 'second key',
    Value: 'first value'
}

但是猫鼬(mongodb的nodejs sdk)告诉我:

But mongoose(nodejs sdk of mongodb) tell me:

"BulkWriteError: E11000 duplicate key error collection: configservice_test.configservice_items_tests index: NamespaceId_1 dup key: { NamespaceId: ObjectId('5f2aaabd4440bb566487cf70') }"

Mongodb 发现 NamespaceId 已经存在,所以它只是抛出异常而不检查左边的 KeyValue.

Mongodb found that NamespaceId has already exists so it just throw an exception without checking left Key and Value.

index概念上与Mysql不同.

Which are different from Mysql on index concept.

我的解决方法是创建一个新的唯一列,该列的值由 NamespaceId, Key, Value 组成,例如 123_first_key_first_value.

My workaround is to create a new single unique column which value is composed by NamespaceId, Key, Value like 123_first_key_first_value.

猫鼬能满足我的需求吗?

Can my needs be achieved by Mongoose?

推荐答案

自己回答.

你必须在创建模型之前创建索引

解决了半天,累了:(

以下代码导致错误

this.model = mongoose.model(item_collection, ItemSchema)
ItemSchema.index({
    'Key': 1,
    'Value': 1,
    'NamespaceId': 1
},{unique: true ,background: true})

代替下面的不是

ItemSchema.index({
    'Key': 1,
    'Value': 1,
    'NamespaceId': 1
},{unique: true ,background: true})
this.model = mongoose.model(item_collection, ItemSchema)

这篇关于如果 A 和 B 相同,则 mongodb 复合索引唯一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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