使用 mongoose 向数据库插入几十万条数据 cpu 直接 100%运行
資深大佬 : liulqiuyu 48
—–代码如下——-
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)
}
});
}
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)
}
});
}
大佬有話說 (1)