gpt4 book ai didi

MongoDB:如何在不中断并发查询的情况下将一大块数据从一个集合移动到另一个集合?

转载 作者:行者123 更新时间:2023-12-05 06:52:40 25 4
gpt4 key购买 nike

我需要将一大块数据 (100Mb) 从 FirstCollection 移动到 MongoDB 中的 SecondCollection。

这两个集合都包含数百万个应保持完整的其他文档。

SecondCollection 已经包含类似的文档。需要删除这些文件。

与此同时,用户正在积极查询 SecondCollection。用户查询 SecondCollection 并且没有收到任何结果或部分替换结果的场景是 Not Acceptable 。

我该怎么做?

到目前为止 $out聚合运算符似乎是一个不错的选择,但似乎无法在 $out 之前的同一操作中删除数据。 .

db.FirstCollection.aggregate([{ $match: {...} }, { $out: SecondCollection }])

Session-Transaction方式似乎是为不同的场景设计的,并不是为了传输这些大数据量,因为默认的事务限制是60秒,不够用。此外,这种方法实际上需要将大量数据从 MongoDB 提取到 NodeJS 应用程序,然后将其写回。

以下是 FirstCollection 中的一些数据示例:

{
_id: ..., // just a regular mongodb ObjectId, it's not important
productName: "Product1",
productId: "product_001", // persistent unique identifier
category: "firstCategory", // only "firstCategory" products should be updated
quantity: 10
// and hundreds of other changing properties like quantity
}
{
_id: ...,
productName: "Product2",
productId: "product_002",
category: "firstCategory",
productQuantity: 20
...
}
{
_id: ...,
productName: "Product3",
productId: "product_003",
category: "firstCategory",
productQuantity: 30
...
}

第二个收藏:

{
_id: ...,
productName: "Product1",
productId: "product_001",
category: "firstCategory",
quantity: 11 // <= this will change to 10
// and hundreds of other changing properties like quantity
}
{
_id: ...,
productName: "Product2",
productId: "product_002",
category: "firstCategory",
productQuantity: 20 // <= this will remain the same
...
}
{
_id: ...,
productName: "Product4",
productId: "product_004", // <= this whole document will be deleted, since there is no "product_004" in the FirstCollection.
category: "firstCategory",
productQuantity: 40
...
}

更新后的 SecondCollection 与 FirstCollection 完全一样:

{
_id: ...,
productName: "Product1",
productId: "product_001",
category: "firstCategory",
quantity: 10
...
}
{
_id: ...,
productName: "Product2",
productId: "product_002",
category: "firstCategory",
productQuantity: 20
...
}
{
_id: ...,
productName: "Product3",
productId: "product_003",
category: "firstCategory",
productQuantity: 30
...
}

最佳答案

使用快照读取关注将所有查询调整为在事务中。

在(另一个)事务中执行更新。

https://github.com/p-mongo/tests/tree/master/query-tx-write

关于MongoDB:如何在不中断并发查询的情况下将一大块数据从一个集合移动到另一个集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65914565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com