Documents "disappearing" from mongodb collection after node loading routine is finished

1.2k views Asked by At

Being new to mongodb and node, this particular issue is just driving me crazy:

I have written a small module, which reads an .csv file, makes it into a JSON array, and loads it into a mongodb collection one record at a time in a loop. As I run this in debug, and set a breakpoint at "var v = i;" line, I can query the mongo collection, and see a fully populated records appear one after another.

However, as soon as the loop is finished - ALL records' data is gone! The actual records are still all there, but they all empty. The data which I just saw in each record is no longer in those records.

It may be some wired scoping issue, but, being new, I just can not tell what that is.

Here is my code:

exports.excelFileParser = function(fileName, tabName, metadataFields){
    var assert = require('assert');
    console.log(metadataFields);
    if(typeof require !== 'undefined') XLSX = require('xlsx');

    var mongodb = require('mongodb');
    var _ = require('underscore');

    var fs = require('fs');

    var Converter=require("csvtojson").core.Converter;

    var  distinctDataFields;

    var MongoClient = mongodb.MongoClient;

    var url = 'mongodb://localhost:27017/datamanager-03-test';

    var fileName = 'clean_file.csv';

    //fs.writeFileSync(fileName, newCsvLines);

    var csvFileName=fileName;
    var fileStream=fs.createReadStream(csvFileName);
//new converter instance
    var csvConverter=new Converter({constructResult:true});

    //end_parsed will be emitted once parsing finished
    csvConverter.on("end_parsed",function(jsonObj){
        //console.log(jsonObj); //here is your result json object
        makeRecords(jsonObj);

    });

    function makeRecords(result){
        console.log(result.length);

        MongoClient.connect(url, function (err, db) {
            if (err) {
                console.log('Unable to connect to the mongoDB server. Error:', err);
            } else {
                console.log('Connection established to', url);

                var collectionName = 'DC_Facilities';

                db.open(function(err, client){
                    client.createCollection(collectionName, function(err, col) {
                    });

                    var collection = db.collection(collectionName);

                    for(var i =0;i < result.length; i++){

                        var dataRecord = result[i];

                        collection.insert(dataRecord);

                        var v = i;
                    }
                    console.log("finished");
                    db.close();
                });
            }
        });
    }

    fileStream.pipe(csvConverter);
};
1

There are 1 answers

0
JohnnyHK On BEST ANSWER

collection.insert is an asynchronous function, so you're calling db.close() before any of them have a chance to complete. You also don't need to call createCollection as the collection will be created for you if it doesn't already exist.

So your code should look something like this instead, so that db.close() isn't called until all the insert operations have completed:

db.open(function(err, client){
    var collection = db.collection(collectionName);
    var inserted = 0;
    for(var i = 0; i < result.length; i++){
        var dataRecord = result[i];
        collection.insert(dataRecord, function(err) {
            if (++inserted == result.length) {
                console.log("finished");
                db.close();
            }
        });
    }
});