First, explain function
The explain function can provide a large number of query-related information, and if it is a slow query, it is the most important diagnostic tool. For example:
Query on fields that have an index:
> db.post.find ({"loc.city": "NY"}). Explain () { "cursor" : "Btreecursor loc.city_1", "Ismultikey" : false, "n" : 0, "Nscannedobjects" : 0, "nscanned" : 0, "Nscannedobjectsallplans" : 0, "Nscannedallplans" : 0, "Scanandorder" : false, "IndexOnly" : false, "Nyields"  : 0, "Nchunkskips" : 0, "Millis" : 1, "Indexbounds" : { "Loc.city" : [ [ "NY", "NY" ] ] }, "Server" : "localhost.localdomain:27017", "Filterset" : false } >
The
Queries on fields that do not have an index:
> db.post.find ({"Name": "Joe"}). Explain () { "cursor" : "basiccursor", "Ismultikey"  : false, "n" : 2, "Nscannedobjects" : 15, "nscanned" : 15, "Nscannedobjectsallplans" : 15, "Nscannedallplans" : 15, "Scanandorder" : false, " IndexOnly " : false, " Nyields " : 0, "Nchunkskips" : 0, "Millis" : 0, "Server" : "localhost.localdomain:27017", "Filterset" : false } >
In contrast to the above two queries, the interpretation of the fields in the explain results:
"Cursor": "Basiccursor" indicates that the query does not use an index; "Btreecursor loc.city_1" means that the index on loc.city is used;
"Ismultikey" indicates whether a multi-key index is used;
"N": the number of documents returned by this query;
"Nscannedobjects": Indicates the number of times the actual document was actually found on the disk according to the index pointer;
"Nscanned": If there is no index, this number is the number of indexed entries searched;
"Scanandorder": Whether the result set is sorted;
"IndexOnly": whether the index can be used to complete the index;
"Nyields": if there is a write operation during the query, the query will be paused; This field represents the number of pauses in the query due to write operations;
"Millis": the number of times the query is spent, the smaller the number, the higher the efficiency of the query;
"Indexbounds": This field describes the usage of the index and gives the scope of the index traversal.
"Filterset": whether to use and index filter;
Second, hint function
If you find that the index used by MongoDB is inconsistent with the index you hope for. , you can use the hit function to force MongoDB to use a specific index. For example
>db.users.find ({"Age": 1, "username":/.*/}). Hint ({"username": 1, "Age": 1})
This article is from the "Margin with Wish" blog, please be sure to keep this source http://281816327.blog.51cto.com/907015/1601477
MongoDB Learning Note 24 explain and hint functions for MongoDB