如何解决使用Node.js将大量JSON插入mongodb集合
我是Node js的新手。 我想调用一个API,并希望将其响应保存到mongodb。
API响应如下
{
page: 1,perPage: 50,count: 37219,status: 'OK',data: [
{
name: 'Abc',email: 'tes@gmail.com',dob: '2020-01-01',country: 'US',address: 'test',city: 'nu'
},{
name: 'Abcd',email: 'tes1@gmail.com',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 举报,一经查实,本站将立刻删除。