python - 带有 upsert = true 的 Mongodb replace_one() 引发重复键错误
问题描述
我已经读过,当我使用 pymongo 的 upsert 并且我不提供“_id”时,upsert 将尝试生成一个新的 Id,这将导致操作失败。这是真的?以及如何在不使用“_id”的情况下进行更新插入?
这是我使用 pymongo 的 replace_one:
db['dataitemdetails'].replace_one({'asset_Id':tdata['asset_id'],'period_type':tdata['period_type'],'detail_id':tdata['detail_id'], 'currencycode':tdata['currencycode'],'dataitem_Id':tdata['dataitem_id'],'period_end':tdata['period_end'], 'scenario_id':tdata['scenario_id'],}, tdata, upsert=True)
我创建了一个复合索引并使用我的搜索条件设置为唯一的,这样在集合中我搜索的内容如果存在,它将始终是唯一的。
并使用 replace_one 我收到此错误:
pymongo.errors.DuplicateKeyError: E11000 duplicate key error collection: finance.dataitemdetails index: asset_id_1_dataitem_id_1_detail_id_1_period_type_1_scenario_id_1_currencycode_1_period_end_1 dup key: { : 19, : 1211, : 0, : "Month", : 1, : "RC", : new Date(949276800000) }
这是我对具有相同过滤器的查找的查询,它返回 1 个文档。
> db.dataitemdetails.find({'asset_id':19,'dataitem_id':1211,'detail_id':0,'period_type':'Month','currencycode':'RC','period_end':new Date(949276800000)})
{ "_id" : ObjectId("5c7721c17314e53a85be7e89"), "Value" : "USD", "period_end" : ISODate("2000-01-31T00:00:00Z"), "currencycode" : "RC", "scenario_id" : 1, "dataitem_id" : 1211, "period_type" : "Month", "detail_id" : 0, "asset_id" : 19 }
我一直不知道该尝试什么以及如何解决这个问题。
完整的错误信息:
Traceback (most recent call last):
File "./periodic_update.sh", line 307, in <module>
db['dataitemdetails'].replace_one({'asset_Id':tdata['asset_id'],'period_type':tdata['period_type'],'detail_id':tdata['detail_id'], 'currencycode':tdata['currencycode'],'dataitem_Id':tdata['dataitem_id'],'period_end':tdata['period_end'], 'scenario_id':tdata['scenario_id'],}, tdata, upsert=True)
File "/usr/local/lib64/python3.7/site-packages/pymongo/collection.py", line 925, in replace_one
collation=collation, session=session),
File "/usr/local/lib64/python3.7/site-packages/pymongo/collection.py", line 851, in _update_retryable
_update, session)
File "/usr/local/lib64/python3.7/site-packages/pymongo/mongo_client.py", line 1248, in _retryable_write
return self._retry_with_session(retryable, func, s, None)
File "/usr/local/lib64/python3.7/site-packages/pymongo/mongo_client.py", line 1201, in _retry_with_session
return func(session, sock_info, retryable)
File "/usr/local/lib64/python3.7/site-packages/pymongo/collection.py", line 847, in _update
retryable_write=retryable_write)
File "/usr/local/lib64/python3.7/site-packages/pymongo/collection.py", line 818, in _update
_check_write_command_response(result)
File "/usr/local/lib64/python3.7/site-packages/pymongo/helpers.py", line 217, in _check_write_command_response
_raise_last_write_error(write_errors)
File "/usr/local/lib64/python3.7/site-packages/pymongo/helpers.py", line 198, in _raise_last_write_error
raise DuplicateKeyError(error.get("errmsg"), 11000, error)
pymongo.errors.DuplicateKeyError: E11000 duplicate key error collection: finance.dataitemdetails index: asset_id_1_dataitem_id_1_detail_id_1_period_type_1_scenario_id_1_currencycode_1_period_end_1 dup key: { : 19, : 1211, : 0, : "Month", : 1, : "RC", : new Date(949276800000) }
```
Find in Mongodb console returned only 1 document
```
>db.dataitemdetails.find({'asset_id':19,'dataitem_id':1211,'detail_id':0,'period_type':'Month','currencycode':'RC','period_end':new Date(949276800000)})
{ "_id" : ObjectId("5c7721c17314e53a85be7e89"), "Value" : "USD", "period_end" : ISODate("2000-01-31T00:00:00Z"), "currencycode" : "RC", "scenario_id" : 1, "dataitem_id" : 1211, "period_type" : "Month", "detail_id" : 0, "asset_id" : 19 }
>
```
Here is my replace one query in the mongo console instead of pymongo:
```
> db.dataitemdetails.replaceOne({'asset_id':19,'dataitem_id':1211,'detail_id':0,'period_type':'Month','currencycode':'RC','period_end':new Date(949276800000)} ,{'asset_id':19,'dataitem_id':1211,'detail_id':0,'period_type':'Month','currencycode':'RC','period_end':new Date(949276800000), 'Value':'USD'}, upsert = true)
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
>
```
some other notes:
If I remove the compound index, the error message would go away, but I would end up with duplicate documents that are the same.
I tried to drop the collection, database and it would end up the same.
解决方案
我发现了问题:
在这行代码中:
db['dataitemdetails'].replace_one({
'asset_Id': tdata['asset_id'],
'period_type': tdata['period_type'],
'detail_id': tdata['detail_id'],
'currencycode': tdata['currencycode'],
'dataitem_Id': tdata['dataitem_id'],
'period_end': tdata['period_end'],
'scenario_id': tdata['scenario_id'],
}, tdata, upsert=True)
我的过滤器是错误的。
其中asset_Id 应该是asset_id,dataitem_Id 应该是dataitem_id。
因此未找到匹配项,但在插入重复键时引发错误。
推荐阅读
- r - 具有动态窗口大小的滚动总和并跳过前几行
- css - 如何在不换行的情况下使 div 中的所有项目等宽?
- java - 如何仅关闭 JDialog 并让 JFrame 仍然可用
- android - 在 Exoplayer 通知中单击暂停按钮时停止动画
- java - 使用 Java 在窗口远程服务器上创建文本文件
- python - 错误:命令“gcc”失败,退出状态为 1 MacOs Catalina
- excel - 如何提取做过安装和维修类工作的人
- python - 如何在 Z3Py 中正确地制作公理?
- swiftui - 无法将“visibleLessonStruct”类型的值转换为预期的参数类型“Binding”
- reactjs - 当 Component.prototype 返回未定义时,如何使用 jest/enzyme 对 componentDidMount 进行单元测试?