MongoDB/Pymongo:UpSert阵列元件
我有以下文档:
{'software_house': 'k1',
'client_id': '1234',
'transactions': [
{'antecedents': 12345,
'consequents': '015896018',
'antecedent support': 0.0030889166727954697},
{'antecedents': '932696735',
'consequents': '939605046',
'antecedent support': 0.0012502757961314996}
...
]}
每个项目中的键“交易”存储在数组3个功能中。
我想更新“交易”数组中包含的每个项目,这些项目与相同的“ software_house”,“ client_id”,“ transactions.antecedents”和“ transactions.constequents”匹配;因此:
- 如果数组中的元素确实存在,则覆盖元素
- 在“交易”中附上一个新值,如果我该
如何使用pymongo实现这一目标?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用Contregation Pipeline进行更新来执行此操作。您可以首先
$ filter
匹配元素。然后$ setunion
带有您要upsertpymongo的项目:
本机mongodb查询:
这是供您参考。
You can do this with an update with aggregation pipeline. You can first
$filter
the element matched. Then$setUnion
with the item you want to upsertPyMongo:
Native MongoDB query:
Here is the Mongo playground for your reference.