使用Node.js将大量JSON插入mongodb集合

问题描述

我是Node js的新手。 我想调用一个API,并希望将其响应保存到mongodb。

API响应如下

{
page: 1,perPage: 50,count: 37219,status: 'OK',data: [
 {
  name: 'Abc',email: '[email protected]',dob: '2020-01-01',country: 'US',address: 'test',city: 'nu'
 },{
  name: 'Abcd',email: '[email protected]',dob: '2020-02-01',city: 'nu'
        
 },//similar 50 records altogether

] }

每次仅显示计数37219中的50条记录。 我想将每页数据作为批量插入mongodb。即 应该先插入第一页50条记录,然后再插入第二页50条记录,依此类推。

    if (!error && response.statusCode == 200) {
            let json =  JSON.stringify(response);
            let result = JSON.parse(JSON.parse(json).body);
            console.log(result); // this will print the json like above
            var totalPage = result.count/result.perPage;
            for(var i = 1; i < totalPage; i++)
            {
             for (var j = 1; j < 50; j++) {
             var todo = new stockTickerModel(
                        {
                         name: result.data[j].name,email: result.data[j].email,dob: result.data[j].dob,country: result.data[j].country,address: result.data[j].address,city: result.data[j].city
                        }
                         
                       );

              // Save it to database
              todo.save(function(err){
              if(err)
              console.log("Error:"+err);
              else
              console.log("success:"+todo);
              })
             }

             }
            }

在37219个文档中,只有36456个文档插入到mongo集合中。其余记录尚未插入。如何将整个37219条记录插入收藏夹?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)