Aggregating operations on collections that exceed millions records.
DBObject match= (dbobject) Json.parse ("{$match: {logtype:{' $in ': [5,9]}}}");D bobject group= (dbobject) Json.parse ("{$ group:{' _id ': ' $domainUrl ', ' count ': {' $sum ': 1}}} '); Aggregationoutput output = Logcollection.aggregate (Match,group);
It happens occasionally .Read timed out exception.
com.mongodb.mongoexception$network: read Operation to server /192.168.10.202:27017 failed on database adlogtableat com.mongodb.dbtcpconnector.innercall (dbtcpconnector.java:253) At com.mongodb.db.command (DB.java:261) At com.mongodb.db.command (db.java:243) Caused by: java.net.sockettimeoutexception: read timed out at java.net.socketinputstream.socketread0 (Native method) at Java.net.SocketInputStream.read (socketinputstream.java:152)
Through multiple tests, found that the average time to perform an aggregation is 5s, more than 5s will be an error!
Then look at the configuration information for MongoDB:
socket-timeout= "//5s"
The default configuration for Socket-timeout is 0, which means there is no limit.
There is no time-out limit, the system is not easy to find a problem, should be based on the actual situation, give a reasonable time-out.
A maximum execution time of 6 seconds was found through multiple tests, setting the time-out to 10000.
Socket-timeout= "10000"//10s
Note: When MongoDB is integrated with spring, if you want to configure multiple mongdb sources, only the last <mongo:options> configuration is enabled.
The parameter configuration information should be stored in the properties file.
<mongo:mongo host= "${mongodb.ip}" id= "mongo202" port= "${mongodb.port}" ><mongo:options Connections-per-host= "threads-allowed-to-block-for-connection-multiplier=" connect-timeout= "1000" Max-wait-time= "auto-connect-retry=" true "socket-keep-alive=" true "socket-timeout=" 10000 "slave-ok=" true " Write-number= "1" write-timeout= "0" write-fsync= "true"/></mongo:mongo>
getting configuration parameters from the Java API
Dbcollection logcollection = mongotemplate.getcollection (collname); Mongooptions mongooptions = Logcollection.getdb (). Getmongo (). Getmongooptions (); System.out.println (Mongooptions.getsockettimeout ());
last point:the difference between ConnectionTimeout and sockettimeout:
A complete request consists of three stages: 1, connection 2, data transfer 3, disconnection
If the connection to the server (in this case the database) is more than ConnectionTimeout, the connectiontimeoutexception is thrown, that is, the server connection times out and the connection is not established within the specified time.
If the connection to the server is successful, the data transfer begins.
If the server takes too long to process the data and exceeds the sockettimeout, it throws Sockettimeoutexceptin, which is the server response time-out and the server does not return to the client data within the specified time.
MongoDB Query Timeout Exception sockettimeoutexception