V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
liulqiuyu
V2EX  ›  Node.js

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

  •  
  •   liulqiuyu · Jan 16, 2020 · 6399 views
    This topic created in 2293 days ago, the information mentioned may be changed or developed.
    2 replies    2020-01-17 09:11:03 +08:00
    liulqiuyu
        1
    liulqiuyu  
    OP
       Jan 16, 2020
    -----代码如下-------
    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  
       Jan 17, 2020
    改成流处理
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4398 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 05:31 · PVG 13:31 · LAX 22:31 · JFK 01:31
    ♥ Do have faith in what you're doing.