我需要过滤一个大的 MongoDB 集合(今天,3500000 个文档,明天,更多......),并将其一些内容传输到一个空集合。这是我的 ES6 幼稚方法:
await col_target.drop();
const cursor = await col_source.find();
while (await cursor.hasNext()) {
const doc = await cursor.next();
// the filter is and array of regular expressions
if (!regex.map(_ => new RegExp(_, 'imu').test(doc.rawJson.text)).reduce((a, b) => a || b)) continue;
await col_target.insertOne(prepareTweet(doc));
}
await db.close();
我感觉这不是最佳的,因为查找和插入操作应该并行化。但我真的不知道该怎么做。有人可以向我提供有关如何改进代码的建议吗?
最佳答案
您应该在单个查询中执行此操作,这样会更快
db.col_source.aggregate([
{$match: {rawJson: /someRegex/gi }},
{$out: "col_target"}
])
当然,别忘了创建一个text index在 rawJson
字段
关于javascript - 将过滤后的文档从 Mongo 集合传输到另一个集合的最佳方法(在 Nodejs 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42996687/