DBCursor
class provides hint() method, which is used to override MongoDB’s default index
selection and query optimization process.
public DBCursor
hint(DBObject indexKeys)
Informs
the database of indexed fields of the collection in order to improve
performance.
public DBCursor
hint(String indexName)
Informs
the database of an indexed field of the collection in order to improve
performance.
> db.students.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "school.students" }, { "v" : 1, "key" : { "last_name" : 1 }, "name" : "last_name_1", "ns" : "school.students" }, { "v" : 1, "key" : { "first_name" : 1 }, "name" : "first_name_1", "ns" : "school.students" }, { "v" : 1, "key" : { "first_name" : 1, "last_name" : 1 }, "name" : "first_name_1_last_name_1", "ns" : "school.students" } ] >
import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBObject; import com.mongodb.MongoClient; public class MongoDBEx { /* Step 1 : get mongoCLient */ public static MongoClient getMongoClient() { MongoClient mongoClient = null; try { mongoClient = new MongoClient("localhost", 27017); } catch (Exception e) { e.printStackTrace(); } return mongoClient; } public static void main(String[] args) throws Exception { MongoClient mongoClient = getMongoClient(); /* Step 2: Connect to DB */ DB db = mongoClient.getDB("school"); /* Step 3 : Get collection */ DBCollection collection = db.getCollection("students"); BasicDBObject obj = new BasicDBObject().append("first_name", "first_name1"); BasicDBObject hint = new BasicDBObject().append("first_name", 1); //String hint = "first_name_1"; DBObject explanation = collection.find(obj).hint(hint).explain(); System.out.println(explanation); /* Close client */ mongoClient.close(); } }
Output
{ "cursor" : "BtreeCursor first_name_1" , "isMultiKey" : false , "n" : 1 , "nscannedObjects" : 1 , "nscanned" : 1 , "nscannedObjectsAllPlans" : 1 , "nscannedAllPlans" : 1 , "scanAndOrder" : false , "indexOnly" : false , "nYields" : 1 , "nChunkSkips" : 0 , "millis" : 73 , "indexBounds" : { "first_name" : [ [ "first_name1" , "first_name1"]]} , "allPlans" : [ { "cursor" : "BtreeCursor first_name_1" , "isMultiKey" : false , "n" : 1 , "nscannedObjects" : 1 , "nscanned" : 1 , "scanAndOrder" : false , "indexOnly" : false , "nChunkSkips" : 0 , "indexBounds" : { "first_name" : [ [ "first_name1" , "first_name1"]]}}] , "server" : "RENT-MIS-LT3016:27017" , "filterSet" : false , "stats" : { "type" : "FETCH" , "works" : 2 , "yields" : 1 , "unyields" : 1 , "invalidates" : 0 , "advanced" : 1 , "needTime" : 0 , "needFetch" : 0 , "isEOF" : 1 , "alreadyHasObj" : 0 , "forcedFetches" : 0 , "matchTested" : 0 , "children" : [ { "type" : "IXSCAN" , "works" : 2 , "yields" : 1 , "unyields" : 1 , "invalidates" : 0 , "advanced" : 1 , "needTime" : 0 , "needFetch" : 0 , "isEOF" : 1 , "keyPattern" : "{ first_name: 1.0 }" , "isMultiKey" : 0 , "boundsVerbose" : "field #0['first_name']: [\"first_name1\", \"first_name1\"]" , "yieldMovedCursor" : 0 , "dupsTested" : 0 , "dupsDropped" : 0 , "seenInvalidated" : 0 , "matchTested" : 0 , "keysExamined" : 1 , "children" : [ ]}]}}
No comments:
Post a Comment