Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB timeout issue (Node.js)

I have an application in Node.JS which basically retrieves a snapshot of data from a WebAPI each 30 minutes. When I try to interrogate the database by using an aggregate query, the app triggers an error related to timeout (MongoError: connection 1 to 127.0.0.1:27017 timed out). From my logs I see that it's exactly 30 seconds. The aggregate query is something like this:

collection.aggregate([{
            "$group": {
                "_id": {
                    country: "$country",
                    user: "$user"
                }
            }
        }]).

I tested the query on a different client (but the same database) and it takes aprox 60 seconds for 25.000.000 records. My assumption is that there is a timeout parameter with the value of 30 seconds that blocks my application from finish the query. Can someone tell me how can I set this parameter up in Node.JS? I'm using 'mongodb' js library and my host machine is Windows.

Since this is a personal project I wanted to investigate how can I use MongoDB instead of a more traditional solution, such as Oracle or MySQL, but this might be a deal breaker since analytics is a core part of the entire application.

like image 643
Bogdan Avatar asked Aug 22 '16 19:08

Bogdan


2 Answers

I found a solution in this answer: "Server x timed out" during MongoDB aggregation.

Basically, the connection string looks like this now:

this.server = 'mongodb://127.0.0.1:27017/test?socketTimeoutMS=90000';

The socketTimeoutMS did the trick and I don't get anymore the connection timeout error.

like image 135
Bogdan Avatar answered Oct 01 '22 15:10

Bogdan


in your mongo connect

let options = {    
    server: {    
      auto_reconnect: true,    
      socketOptions: {

        keepAlive: 1,    
        connectTimeoutMS: 60000,    
        socketTimeoutMS: 60000,    
      }    
    },    
    replset: {    
      auto_reconnect: true,    
      socketOptions: {    
        keepAlive: 1,    
        connectTimeoutMS: 60000,    
       socketTimeoutMS: 60000,    
      }    
   }

  }

  Mongoose.connect(url, options);
like image 30
David Zambrano Avatar answered Oct 01 '22 13:10

David Zambrano