Use Mongoose insert about 70 objects with MongoError:read ECONNRESET

764 Views Asked by At

In my scenario, node need read a array of img files in a directory, and store binaries to MongoDB. This function is to test the ability of store file to MongoDB. The img file size range 5kb to 3mb.

Part 1. connect mongdb

I use the options parameter to set the socket's keepAlive, I want to the socket keep long.

var options = { 
    server: { 
        socketOptions: { 
            keepAlive: 1,
            connectTimeoutMS: 30000
        }
    },
    replset: {
        socketOptions: {
            keepAlive: 1,
            connectTimeoutMS : 30000
        } 
    } 
};

mongoose.connect(mongooseURI, options);

Part 2. insert operation

fs can read the files successfully, but the number of files may cause a error, i will write lately. I use the collection.insert to store a array of data.

Img.collection.insert(fileArr, function(err, docs){
    if(err){
        console.info('\033[31mError: Failed to save docs  with error: ', err, '\033[39m');
        process.emit('DB_OPS_DONE', err.message);
    }else{
        console.info('%d images were successfully stored.', docs.length);
        console.log('benchmark took %d nanoseconds', diff[0] * 1e9 + diff[1]);
        process.emit('DB_OPS_DONE', 'Successful insert docs');
    }
});

Part 3. close connection

This make the node app stop normally whether the error come up.

process.on('DB_OPS_DONE', function(msg) {
    mongoose.connection.close(function () {
        console.log('Mongoose disconnected on app termination with ', msg);
        process.exit(0);
    });
});

I get a error if the number of files become large. Number test out about 40.That means if the number of files < 40, the node can finish successfully.

The error is MongoError: server localhost:27017 received an error {"name":"MongoError","message":"read ECONNRESET"}


environment:

os: win7

nodejs: 0.10.31

mongodb: 2.7

mongoose: 4.0.5

0

There are 0 best solutions below