[英]Using dictionary key as the _id when using pymongo
I have a dictionary like so:我有一本这样的字典:
a_dictionary = {
'UniqueNumber': 1,
'YearResults' : [],
}
When I use:当我使用:
MyCollection.replace_one(
{'UniqueNumber': a_dictionary['UniqueNumber']}, a_dictionary, upsert=True)
Mongodb is creating an _id
field: _id: 5e467caa3cec0556ddcb3a41. Mongodb 正在创建一个
_id
字段:_id: 5e467caa3cec0556ddcb3a41。
I don't want this, I want to use my UniqueNumber and have no _id field.我不想要这个,我想使用我的 UniqueNumber 并且没有 _id 字段。
As per the documentation: Insert document根据文档: 插入文档
When a document is inserted a special key, "_id", is automatically added if the document doesn't already contain an "_id" key.
插入文档时,如果文档不包含“_id”键,则会自动添加一个特殊键“_id”。 The value of "_id" must be unique across the collection
“_id”的值在整个集合中必须是唯一的
So in order to have your custom identifier you can add _id
to the record you are inserting.因此,为了拥有您的自定义标识符,您可以将
_id
添加到您插入的记录中。 Please note The value of "_id" must be unique across the collection
请注意
The value of "_id" must be unique across the collection
Hope it helps.希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.