使用Pymongo更新和添加多个文档

发布于 2025-01-21 06:11:57 字数 1882 浏览 2 评论 0原文

我有一个Mongo数据库,具有下面的数据记录

[
    {"name" : "a", "email" : "[email protected]","ml_pred":"valid","hum_pred":"null", "score":0.92},
    {"name" : "b","email" : "[email protected]","ml_pred":"invalid","hum_pred":"null", "score":0.2},
    {"name" : "c","email" : "[email protected]","ml_pred":"null","hum_pred":"null"},
    {"name" : "d","email" : "[email protected]","ml_pred":"null","hum_pred":"null"},
    {"name" : "e","email" : "[email protected]","ml_pred":"null","hum_pred":"null"}
]

,使用Pymongo中的insert_many插入此数据,就像

from pymongo import MongoClient
client = MongoClient('mongodb://testuser:testuser@mongo:27017/testdb?authSource=admin')
mydb = client["testdb"]    #Mongo database
mycol = mydb["todos"]    #Mongo Collection Name
mycol.insert_many(record)

如何进行批量更新,并在单个步骤中添加新字段在第三和第4个文档中,以下数据

[
    {"name" : "c","email" : "[email protected]","ml_pred":"valid","hum_pred":"null","score":0.83},
    {"name" : "d","email" : "[email protected]","ml_pred":"invalid","hum_pred":"null","score":0.12}
]

I have a mongo database having data record like below

[
    {"name" : "a", "email" : "[email protected]","ml_pred":"valid","hum_pred":"null", "score":0.92},
    {"name" : "b","email" : "[email protected]","ml_pred":"invalid","hum_pred":"null", "score":0.2},
    {"name" : "c","email" : "[email protected]","ml_pred":"null","hum_pred":"null"},
    {"name" : "d","email" : "[email protected]","ml_pred":"null","hum_pred":"null"},
    {"name" : "e","email" : "[email protected]","ml_pred":"null","hum_pred":"null"}
]

This data is inserted using insert_many in pymongo like

from pymongo import MongoClient
client = MongoClient('mongodb://testuser:testuser@mongo:27017/testdb?authSource=admin')
mydb = client["testdb"]    #Mongo database
mycol = mydb["todos"]    #Mongo Collection Name
mycol.insert_many(record)

How is it possible to do bulk update and add new fields in a single step to the 3rd and 4th document with the following data

[
    {"name" : "c","email" : "[email protected]","ml_pred":"valid","hum_pred":"null","score":0.83},
    {"name" : "d","email" : "[email protected]","ml_pred":"invalid","hum_pred":"null","score":0.12}
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏了南城 2025-01-28 06:11:57

您可以发送2个更新。您还可以使它们成为批量更新,也可以通过1个更新来编写管道更新,但是我认为您在这里不需要更复杂的事情。

两者都匹配电子邮件,然后更新2个字段。使用更新$ set运算符。

query1

playmongo

update(
{"email": {"$eq": "[email protected]"}},
{"$set": {"ml_pred": "valid", "score": 0.83}})

query2 query2 query2

playmongo

update(
{"email": {"$eq": "[email protected]"}},
{"$set": {"ml_pred": "invalid", "score": 0.12}})

编辑

我不使用pymongo,我猜你需要 this ,但这取决于您使用的python驱动程序,但在所有情况下都很简单

requests = [
    UpdateOne(...query1...),
    UpdateOne(...query2...)]
try:
    db.test.bulk_write(requests, ordered=False)
except BulkWriteError as bwe:
    pprint(bwe.details)

You can send 2 updates.You could also make them a bulk update, or you could also write a pipeline update to do both with 1 update, but i think you dont need more complicated things here.

Both match the email, and then update the 2 fields.Using the update $set operator.

Query1

Playmongo

update(
{"email": {"$eq": "[email protected]"}},
{"$set": {"ml_pred": "valid", "score": 0.83}})

Query2

Playmongo

update(
{"email": {"$eq": "[email protected]"}},
{"$set": {"ml_pred": "invalid", "score": 0.12}})

Edit

I don't use pymongo i guess you need something like this but this depends on the python driver you use also, but it will be simple in all cases

requests = [
    UpdateOne(...query1...),
    UpdateOne(...query2...)]
try:
    db.test.bulk_write(requests, ordered=False)
except BulkWriteError as bwe:
    pprint(bwe.details)
小ぇ时光︴ 2025-01-28 06:11:57

我添加了多个更新和字段添加(基于字段电子邮件

email_id=[data.pop("email") for data in datas]
operations=[UpdateOne({"email":email},{'$set':data}) for email ,data in zip(email_id,datas)]
mycol.bulk_write(operations)

I added multiple update and adding of fields by (based on the field email)

email_id=[data.pop("email") for data in datas]
operations=[UpdateOne({"email":email},{'$set':data}) for email ,data in zip(email_id,datas)]
mycol.bulk_write(operations)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文