提高MongoDB的更新速度

发布于 2025-02-06 21:06:58 字数 548 浏览 0 评论 0原文

我在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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文