我有一本像这样的字典:
a_dictionary = {
'UniqueNumber': 1,
'YearResults' : [],
}
当我使用时:
MyCollection.replace_one(
{'UniqueNumber': a_dictionary['UniqueNumber']}, a_dictionary, upsert=True)
Mongodb 正在创建一个 _id
字段:_id:5e467caa3cec0556ddcb3a41。
我不想要这个,我想使用我的 UniqueNumber 并且没有 _id 字段。
最佳答案
根据文档:Insert document
When a document is inserted a special key, "_id", is automatically added if the document doesn’t already contain an "_id" key. The value of "_id" must be unique across the collection
因此,为了获得自定义标识符,您可以将 _id
添加到您要插入的记录中。请注意“_id”的值在整个集合中必须是唯一的
希望有帮助。
关于python - 使用 pymongo 时使用字典键作为 _id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60225113/