V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
liulqiuyu
V2EX  ›  Node.js

使用 mongoose 向数据库插入几十万条数据 cpu 直接 100%运行

  •  
  •   liulqiuyu · 2020-01-16 16:44:10 +08:00 · 5549 次点击
    这是一个创建于 1533 天前的主题,其中的信息可能已经有所发展或是发生改变。
    2 条回复    2020-01-17 09:11:03 +08:00
    liulqiuyu
        1
    liulqiuyu  
    OP
       2020-01-16 16:47:49 +08:00
    -----代码如下-------
    function readByfile(value,mongodb){
    let readPath=path.resolve(__dirname,'json/json/'+value);
    let data = fs.readFileSync(readPath,'utf-8');
    mongodb.insertMany(JSON.parse(data),function(err){
    console.log(err);
    });
    }
    function readDir(dirpath){
    let files = fs.readdirSync(dirpath);
    _.each(files , function(value){
    if(value.startsWith('authors.song')){
    readByfile(value,song_author)
    }else if(value.startsWith('authors.tang')){
    readByfile(value,tang_author)
    }else if(value.startsWith('poet.tang')){
    readByfile(value,tang_poet)
    }else if(value.startsWith('poet.song')){
    readByfile(value,song_poet)
    }
    });
    }
    lovemegowin
        2
    lovemegowin  
       2020-01-17 09:11:03 +08:00
    改成流处理
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   991 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:08 · PVG 04:08 · LAX 13:08 · JFK 16:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.