我需要将一个大数据块(100Mb)从FirstCollection移动到MongoDB的SecondCollection中。
两个集合包含成千上万的其他文档,这些文档应保持不变。
SecondCollection已经包含类似的文档。这些文档需要被删除。
同时,SecondCollection正在被用户积极查询。用户查询SecondCollection并且没有结果或部分替换的结果的情况是不可接受的。
我该怎么做?
到目前为止,$out聚合操作符似乎是一个很好的选择,但在$out之前似乎没有删除数据的方法。
更新后的 SecondCollection 看起来与 FirstCollection 完全相同。
两个集合包含成千上万的其他文档,这些文档应保持不变。
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
...
}