简体   繁体   中英

Proper way to loop through items in large JSON, modify them and insert one by one into a collection in mongodb

I'm getting a large JSON from a request to an url, then I want to loop through all of them and make some changes in its JSON objects and, here is where I'm having problems, I want to insert each modified JSON item to a collection in the mongodb.

I'm totally new to all that technologies, nodejs, express and mongodb. Will appreciate all the possible guidance in this process to learn to develop on this technologies.

//The request that gets the JSON and sends to the function to handle it
async.auto({
    'gatheringteams': [function (callback) {

        request(url, function (error, response, body) {
            if (!error && response.statusCode == 200) {
                var sendresults = require('./public/javascripts/teams.js');
                sendresults.storeTeams(body);
            } else {
                console.log('error' + response.statusCode);
            }
        });

        callback(null);
    }],
},
    function (err, results) {
        if (err) {
            console.log(err);
        }
    }
);



//The function receiving the JSON
function storeTeams(results) {
    var parsed = JSON.parse(results);
    var getdbdefinition = require('./db');
    var dbconnection = getdbdefinition.getDb();
    var db = dbconnection.db('teamsdb');

    for (let key in parsed) {
        let parsedobject = parsed[key];
        db.collection('players').insertOne(parsedobject, function (err, result) {

            if (err) {
                console.log('Error!')
            }

            if(result){
                console.log('Inserted!');
            }

        });
    }
};

The problem is that, for some reason, insertone is inserting only 50 items, the rest are not inserted and I'm not getting errors to have a clue what I'm doing wrong. (as you can see I didn't modify the JSON objects, I try to insert them as they are, but doesn't work either)

I am getting over 185k records and it can grow with time. Storing the objects in an array is what I used.

  1. I get the JSON from the API and send it to the function.
  2. Parsed that JSON.
  3. Get the database connection.
  4. Loop through the parsed JSON and add the items to an array.
  5. Get all the items in the collection.
  6. When the items in the collection are returned is the time to loop through the array with the parsed items where I check if the item exist in the database or is new.

    db.collection('players').find({}).toArray((err, items) => {

     if (err) { logger.debug(err); } if (items) { for (var i = 0; i < listitems.length; i++) { var finditem = items.find(function (element) { return element['Name'] === listitems[i]['Name']; }); if (finditem == null) { resultitems.push(listitems[i]); } else { // Modify if needed the item } } if (listitems.length > 0) { db.collection('players').deleteMany({}).then(function (r) { db.collection('players').insertMany(listitems, function (err, result) { if (err) { logger.debug('Error!') } if (result) { logger.info('Data inserted!'); } }); }); } else { logger.info('No data to insert.') } } 

    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM