mongoose 如何一次性更新大量文档?急。

2018-04-24 17:04:10 +08:00
 LeungJZ

在做 node 爬虫。

目前用的是 BulkWrite :

const Book = mongoose.model('Book', bookSchema);
exports.saveAll = (from_en, books) => {
    const bulkWrite = books.map(book => ({
        replaceOne: {
            filter: {
                from_en,
                originId: book.originId
            },
            replacement: book,
            upsert: true
        }
    }))
    return Book.bulkWrite(bulkWrite).catch(error => console.error(error))
}

然后发现,这么处理 11200 条数据耗时 600s:

catId: 82 from 5040 to 5600. crawl cost: 10.1min, dataTotal: 11200, upsertTotal: 11000, matchTotal: 200
mongodb is disonnected
mongodb: 603757.883ms
✨  Done in 604.47s.

这个该如何优化?

下面是部分爬虫逻辑的代码: while 内部的代码

机子性能:I7 6700HQ / 16G RAM

4352 次点击
所在节点    Node.js
3 条回复
LeungJZ
2018-04-24 17:05:52 +08:00
额,必须充钱才能置顶吗?
yiding
2018-04-25 01:39:21 +08:00
LeungJZ
2018-04-25 09:14:28 +08:00
@yiding
不顺序执行的话,如何知道执行完成?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/449487

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX