简体   繁体   中英

Error in fetching data from MongoDB in Node.js

I am trying to fetch data from MongoDB, where database connection is ok everytime, there isn't any error. But when I am trying to fetch data it gives me the error below:

 { [MongoError: connection 5 to c1669.candidate.18.mongolayer.com:11669 timed out]
   name: 'MongoError',
   message: 'connection 5 to c1669.candidate.18.mongolayer.com:11669 timed out' }

But first 10 to 11 attempts were good and was getting expected data from mongo, later I introduced by above error continuously.

For more info: Using mongodb npm module .

I think its connection pool size problem as you are saying first 10-11 attempts are good so may be you have defined very limited pool size for connections so when you are creating connections, after exceeding to limit it is not allowing to query the database. Please check it as I am not sure without seeing the code but this might be the reason.

Update:

Try below options at the time of connecting to db.

var dbOptions = {
    server: {
        socketOptions: {
            keepAlive: 100,
            connectTimeoutMS: 30000
        }
    }
};

mongoose.connect('mongodb://localhost/db', dbOptions);

Please refer to this link as it seems that you are using replicaset. https://github.com/Automattic/mongoose/issues/2089

Please update to mongodb driver 2.2.10+ version - because there were some minor issues with connection-pool refactoring between 2.1.11–2.2.4 versions.

See

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