gpt4 book ai didi

arrays - 如何为嵌套在数组中的文档中的字段设置唯一约束?

转载 作者:行者123 更新时间:2023-12-04 12:59:04 24 4
gpt4 key购买 nike

我在 MongoDB 中有一组文档,如下所示:

{"_id": 1, "array": [{"id": 1, "content": "..."}, {"id": 2, "content": "..."}]}
{"_id": 2, "array": [{"id": 1, "content": "..."}, {"id": 2, "content": "..."}, {"a_id": 3, "content": "..."}]}

我想确保没有重复 array.id在每个文档中。所以提供的例子没问题,但followign不是:
{"_id": 1, "array": [{"id": 1, "content": "..."}, {"id": 1, "content": "..."}]}

我的问题是如何做到这一点(最好在 PyMongo 中)。

编辑

我尝试的是以下我认为会在 (_id, array.id) 上创建 key 的代码但如果你运行它,这不会发生:
from pymongo import MongoClient, ASCENDING

client = MongoClient(host="localhost", port=27017)
database = client["test_db"]
collection = database["test_collection"]
collection.drop()

collection.create_index(keys=[("_id", ASCENDING),
("array.id", ASCENDING)],
unique=True,
name="new_key")

document = {"array": [{"id": 1}, {"id": 2}]}
collection.insert_one(document)

collection.find_one_and_update({"_id": document["_id"]},
{"$push": {"array": {"id": 1}}})

updated_document = collection.find_one({"_id": document["_id"]})

print(updated_document)

哪个输出(请注意,在 id = 1 中有两个带有 array 的对象)。我希望得到一个异常(exception)。
{'_id': ObjectId('5eb51270d6d70fbba739e3b2'), 'array': [{'id': 1}, {'id': 2}, {'id': 1}]}

最佳答案

So if I understand it correctly there is no way how to set index (or some condition) that would enforce the uniqueness within the document, right? (Other than check this explicitly when creating the document or when inserting into it.)



是的。请参阅以下两个关于在带有嵌入文档的数组字段上使用唯一索引的场景。

Unique Multikey Index (数组中嵌入文档字段的索引):

For unique indexes, the unique constraint applies across separate documents in the collection rather than within a single document.

Because the unique constraint applies to separate documents, for a unique multikey index, a document may have array elements that result in repeating index key values as long as the index key values for that document do not duplicate those of another document.



第一个场景:
db.arrays.createIndex( { _id: 1, "array.id": 1}, { unique: true } )

db.arrays.insertOne( { "_id": 1, "array": [ { "id": 1, "content": "11"}, { "id": 2, "content": "22"} ] } )

db.arrays.insertOne( { "_id": 2, "array": [ { "id": 1, "content": "1100"}, { "id": 5, "content": "55"} ] } )

db.arrays.insertOne( {"_id": 3, "array": [ {"id": 3, "content": "33"}, {"id": 3, "content": "3300"} ] } )

所有三个文档都被插入而没有任何错误。

根据 上的说明唯一多键索引 ,上面,带有 _id : 3 的文档数组中有两个嵌入的文档,具有相同的 "array.id"值: 3 .

此外,对复合索引 { _id: 1, "array.id": 1} 的两个键强制执行唯一性。并且有重复的 "array.id"文档中的值也( _id12 )。

第二个场景:
db.arrays2.createIndex( { "array.id": 1 }, { unique: true } )

db.arrays2.insertOne( { "_id": 3, "array": [ { "id": 3, "content": "33" }, { "id": 3, "content": "330"} ] } )
db.arrays2.insertOne( { "_id": 4, "array": [ { "id": 3, "content": "331" }, { "id": 30, "content": "3300" } ] } )

第一个文档 _id : 3插入成功。第二个有错误: "errmsg" : "E11000 duplicate key error collection: test.arrays2 index: array.id_1 dup key: { array.id: 3.0 } " .此行为符合注释 的预期。唯一多键索引 .

关于arrays - 如何为嵌套在数组中的文档中的字段设置唯一约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61655391/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com