0

Just a bit of background, MongoDB does not crash if I use insertMany instead.

So here's the relevant code:

var propertiesObject = { results: 5000 };

request({url: "https://randomuser.me/api", qs:propertiesObject}, function(err, r, body) {
    if(err) { console.log(err); return; }
    console.log("Get response: " + r.statusCode);

    let j = JSON.parse(r.body)

    let id = 1
    let users = j.results

    for (let user of users)
    {
        MongoClient.connect("mongodb://localhost:27017/test", function (err, db) {
            db.collection('Persons', function (err, collection) {
                collection.insert({ id, first_name: user.name.first, lastName: user.name.last })
            })
        })
        id++
    }
});

This causes MongoDB to crash, which requires me to run a restart of it. Is there any particular reason why it would do that? It won't crash if it's a smaller value e.g. 10

Styx
  • 9,863
  • 8
  • 43
  • 53
A. L
  • 11,695
  • 23
  • 85
  • 163
  • 1
    Suggestion: @A.Lau, you should try mongoimport function of MongoDB to import bulk of data in a single shot. Here link to import list of data from JSON file: https://stackoverflow.com/questions/15171622/mongoimport-of-json-file – Ankit Rana Oct 13 '17 at 06:34

1 Answers1

1

try like this

MongoClient.connect("mongodb://localhost:27017/test", function (err, db) {
    for (let user of users){
       db.collection('persons').insert({
        id, first_name: user.name.first, lastName: user.name.last });

      id++;
    }
  })
Rohit Kumar
  • 1,777
  • 2
  • 13
  • 26
Ravi Teja
  • 649
  • 7
  • 17