The production environment instance frequently OOM, the investigation discovery mainly by some $near query causes, the query resembles follows, in which the LATLNG field has established 2d index.
{
"Find": "UserData",
' Filter ': {
"LATLNG": {
"$near": [
116.34642045073839,
39.87082232130999
],
"$maxDistance": 0.9009009009009009
}
},
"Ntoreturn": 10000
}
The official explanation is that the $near need to sort, actually from the center point, constantly going out to find the nearby points, so $near cursor will cache the results. The default timeout for MongoDB cursor is 10 minutes, so if the total amount of memory that is open cursor cache exceeds the total memory in 10 minutes, OOM will occur.
For the above problems, the application has the following 2 ways to optimize memory usage
Set a smaller cursor timeout
Set Mongod–setparameter cursortimeoutmillis=60000 on startup, or you can put it in a configuration file
Modify Db.runcommand Online ({setparameter:1, cursortimeoutmillis:60000})
Find time to set the {singebatch:true} option, so that Mongod immediately after the query shutdown cursor, this method only applies to the cursor do not need to Getmore query.