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

3
我需要将一个大数据块(100Mb)从FirstCollection移动到MongoDB的SecondCollection中。
两个集合包含成千上万的其他文档,这些文档应保持不变。
SecondCollection已经包含类似的文档。这些文档需要被删除。
同时,SecondCollection正在被用户积极查询。用户查询SecondCollection并且没有结果或部分替换的结果的情况是不可接受的。
我该怎么做?
到目前为止,$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
  ...
}

M10还不错,尽管低IOPS可能会是一个问题。您能确认版本吗?M10有3.6、4.0、4.2和4.4可用。它们在查询语言方面具有略微不同的功能。我问的是定义“相似性”的逻辑,而不是数量。它是精确复制、唯一字段匹配、非唯一字段匹配还是字段组合匹配?哪些符合“相似”并且必须用FirstCollection中的文档替换? - Alex Blex
版本号为4.4。至于相似性:SecondCollection包含昨天的数据。SecondCollection中99%的文档具有匹配的“名称”和“自定义ID”属性,但其他属性不同。在FirstCollection和SecondCollection之间没有具有100%相等属性值的文档。 - stkvtflw
1
我不需要在查询范围内获取 SecondCollection 的数据,就像问题描述的那样。此外,删除和重命名会导致停机时间。 - stkvtflw
如果停机时间和部分一致性都是问题,那么为什么不处理并克隆数据到一个全新的集合(“merge_collection”),然后将应用程序指向该集合呢?在此期间,考虑到文集的只读行为。否则,从一个集合复制数据到另一个集合在过程执行期间仍会产生一些不一致性(最终一致性)。 - Naman
因为我需要同时并行更新多个“类别”。 - stkvtflw
显示剩余8条评论
1个回答

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接