提高MongoDB的更新速度
我在MongoDB收藏了100万个文件。每隔几个小时,我就需要使用几百个新文档更新集合。每个文档都有一个相应的唯一“ house-id_listing”,我用作唯一的“ id”。如果集合中存在“ house-id_listing”,我想用新文档替换它,否则,我只是插入集合中当前不存在的新文档。我正在使用以下Pymongo代码来做到这一点:
bulk_ops = []
for listing_id, new_value in data.items():
bulk_ops.append(
pymongo.UpdateOne(
{'house-id_listing': listing_id},
{"$set": new_value}
,upsert=True
)
)
result = db['detached'].bulk_write(bulk_ops)
print(result.bulk_api_result)
但是,它非常慢。更新300个文档需要将近一个小时,这太慢了。我如何提高此速度?
I have a collection in mongodb with 1 million documents. Every few hours, I need to update the collection with a few hundred new documents. Each document has a corresponding unique 'house-id_listing' which I use as the unique "id". If the 'house-id_listing' exists in the collection, I want to replace it with the new document, otherwise, I simply insert the new document which does not currently exist in the collection. I am using the following pymongo code to do this:
bulk_ops = []
for listing_id, new_value in data.items():
bulk_ops.append(
pymongo.UpdateOne(
{'house-id_listing': listing_id},
{"$set": new_value}
,upsert=True
)
)
result = db['detached'].bulk_write(bulk_ops)
print(result.bulk_api_result)
However, it is extremely slow. It takes almost an hour to update 300 documents, which is way too slow. How can I improve the speed of this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论