turn on the Profiling function To optimize for slow queries:
MongoDB can monitor data through profile to optimize it.
To see whether the profile function is currently open with commands
Db.getprofilinglevel () returns level with a value of 0|1|2, meaning: 0 for off, 1 for slow command, 2 for all
Db.setprofilinglevel (level); #level等级, value ibid.
At level 1, the slow command defaults to 100ms and changes to Db.setprofilinglevel (LEVEL,SLOWMS) such as Db.setprofilinglevel (1,50) to 50 milliseconds.
View the current monitoring log through Db.system.profile.find ().
Such as:
> Db.system.profile.find ({millis:{$gt: $}}) {"TS": Isodate ("2011-07-23t02:50:13.941z"), "info": "Query Order.order reslen:11022 nscanned:672230 \nquery: {status:1.0} nreturned:101 bytes:11006 640ms "," Millis ": 640} { "TS": Isodate ("2011-07-23t02:51:00.096z"), "info": "Query Order.order reslen:11146 nscanned:672302 \nquery: { status:1.0, User.uid: {$gt: 1663199.0}} nreturned:101 bytes:11130 647ms "," Millis ": 647}
The meaning of the value here is
ts: Command Execution time
Info: Contents of the command
Query: Representative queries
Order.order: The Library and collection representing the query
Reslen: Returned result set size, byte number
nscanned: Number of scanned records
Nquery: The following is the query condition
Nreturned: Returns the number of records and the elapsed time
Millis: The time spent
If you find that time is longer, you need to optimize it.
For example, if the number of nscanned is large or close to the total number of records, then the index query may not be used.
The Reslen is large, and it is possible to return unnecessary fields.
Nreturned is very large, it is possible to query the time without adding restrictions.
Translated from: http://my.oschina.net/baowenke/blog/97756