【问题标题】:MongoDB Unique Index on nested objects嵌套对象上的 MongoDB 唯一索引
【发布时间】:2020-09-12 22:24:24
【问题描述】:

这是我的 MongoDB 集合:

{
    "_id": "0",
    "profiles": {
        "A123": {
            "name": "Mike"
        },
        "B456": {
            "name": "John"
        }
}

我想确保“名称”字段不能具有相同的值。如果有人叫迈克,没有人可以叫迈克

如果我尝试在“配置文件”中的此集合内创建一个名称已在使用中的新对象,如何创建一个会引发错误的索引?

【问题讨论】:

  • 为什么你没有制作个人资料集合和“名称”作为一个更有说服力的属性。之后,您可以使用 Collection.findOne({name:name}) 查询数据库,如果它已经存在或不存在,请执行相关操作。

标签: mongodb unique-index


【解决方案1】:

以您当前的结构,您无法做到。 unique index 这样您可以使用某个嵌套值作为引用作为唯一索引的点,以区分文档而不是单个文档。

您可以执行以下两种操作之一:

  1. 更改文档结构。将您的文档更改为:
[
  {
    "_id": "0",
    "profile": {
      "name": "Mike",
      "key": "A123"
    }
  },
  {
    "_id": "1",
    "profile": {
      "name": "John",
      "key": "B456"
    }
  }
]

使用这样的结构,您可以在 profile.name 上创建唯一索引,如下所示:

db.collection.createIndex({"profile.name" : 1}, { unique: true } );
  1. 拥有另一个名为 names 的集合并将其用作您的参考。请注意,如果您经常更新名称,这可能会导致大量开销操作。如果不是这样,这将是解决您的问题的一个非常快速和简单的解决方案,因为您只需在插入/删除操作时更新它。

【讨论】:

  • 谢谢,我认为这是最相关的事情,因为我开始使用 MongoDB 架构。
【解决方案2】:
//code from output of windows mongo shell client CLI(command line interface)
//create a collection for profiles with objects shown
db.test5.insertMany(
[
 {
    "_id": "0",
    "profiles": {
        "A123": {
            "name": "Mike"
        },
        "B456": {
            "name": "John"
            }
   }
   }
]);
> db.test5.find().pretty();
{
        "_id" : "0",
        "profiles" : {
                "A123" : {
                        "name" : "Mike"
                },
                "B456" : {
                        "name" : "John"
                }
        }
}
>
> db.test5.find().pretty();
{
        "_id" : "0",
        "profiles" : {
                "A123" : {
                        "name" : "Mike"
                },
                "B456" : {
                        "name" : "John"
                }
        }
}
>//create index with the path leading to name, 
//"*" is used as its not unique within the object path
//check index is created
> db.test5.getIndexes();
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "test.test5"
        },
        {
                "v" : 2,
                "key" : {
                        "profiles.*.name" : 1
                },
                "name" : "profiles.*.name_1",
                "ns" : "test.test5"
        }
]
//test duplicate document insertion for "Mike" as document exists for "Mike"
> db.test5.insertOne(
...  {
...     "_id": "0",
...     "profiles": {
...         "A123": {
...             "name": "Mike"
...         }
...      }
...    }
... );
2020-09-13T13:23:04.908+0530 E  QUERY    [js] WriteError({
        "index" : 0,
        "code" : 11000,
        "errmsg" : "E11000 duplicate key error collection: test.test5 index: _id_ dup key: { _id: \"0\" }",
        "op" : {
                "_id" : "0",
                "profiles" : {
                        "A123" : {
                                "name" : "Mike"
                        }
                }
        }
}) :
WriteError({
        "index" : 0,
        "code" : 11000,
        "errmsg" : "E11000 duplicate key error collection: test.test5 index: _id_ dup key: { _id: \"0\" }",
        "op" : {
                "_id" : "0",
                "profiles" : {
                        "A123" : {
                                "name" : "Mike"
                        }
                }
        }
})
WriteError@src/mongo/shell/bulk_api.js:458:48
mergeBatchResults@src/mongo/shell/bulk_api.js:855:49
executeBatch@src/mongo/shell/bulk_api.js:919:13
Bulk/this.execute@src/mongo/shell/bulk_api.js:1163:21
DBCollection.prototype.insertOne@src/mongo/shell/crud_api.js:264:9
@(shell):1:1
//re-check the collection, no duplicates are inserted
> db.test5.find().pretty();
{
        "_id" : "0",
        "profiles" : {
                "A123" : {
                        "name" : "Mike"
                },
                "B456" : {
                        "name" : "John"
                }
        }
}
>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-16
    • 2020-09-22
    • 1970-01-01
    • 2014-11-15
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多