HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
MongoDB query and analysis to ensure the effectiveness of our proposed index is an important tool to query performance analysis.
MongoDB query analysis commonly used functions are: explain () and hint ().
explain operation provides query information, use the index and query statistics. Help us to optimize the index.
Next we create an index gender and user_name users in the collection:
>db.users.ensureIndex({gender:1,user_name:1}) </p> <p>现在在查询语句中使用 explain :</p> <pre> >db.users.find({gender:"M"},{user_name:1,_id:0}).explain()
Above explain () query returns the following results:
{ "cursor" : "BtreeCursor gender_1_user_name_1", "isMultiKey" : false, "n" : 1, "nscannedObjects" : 0, "nscanned" : 1, "nscannedObjectsAllPlans" : 0, "nscannedAllPlans" : 1, "scanAndOrder" : false, "indexOnly" : true, "nYields" : 0, "nChunkSkips" : 0, "millis" : 0, "indexBounds" : { "gender" : [ [ "M", "M" ] ], "user_name" : [ [ { "$minElement" : 1 }, { "$maxElement" : 1 } ] ] } }
Now, we look at the field of the result set:
While MongoDB query optimizer generally work very well, but you can also use hints to force MongoDB to use a specified index.
This approach will improve performance in some cases. An index of the collection and execute the query more than one field (some fields have been indexed).
Examples use the following query specifies the gender and user_name index fields to the query:
>db.users.find({gender:"M"},{user_name:1,_id:0}).hint({gender:1,user_name:1})
You can use the explain () function to parse the above query:
>db.users.find({gender:"M"},{user_name:1,_id:0}).hint({gender:1,user_name:1}).explain()