MongoDB索引:totalDocsExamined返回0

时间:2015-07-02 05:38:33

标签: mongodb mongodb-java

我使用Mongo Java驱动程序编写了一个MongoDB索引,如下所示:

MongoClient mongoClient = new MongoClient("localhost", 27017);

DB db = mongoClient.getDB("test");

DBCollection coll = db.getCollection("indexdemo");

coll.createIndex(new BasicDBObject("code",1));

for (int ii=0;ii<100000;ii++) {

DBObject doc = new BasicDBObject("code", ii);

coll.insert(doc);

}

当我执行索引的解释计划(MongoDB 3.x)时,似乎可以识别索引边界但是totalKeysExamined是0并且totalDocsExamined也是0

db.indextest.find({code: 5000}).explain("executionStats")
{  
. . . .
   "executionStats":{  
      "executionSuccess":true,
      "nReturned":0,
      "executionTimeMillis":0,
      "totalKeysExamined":0,
      "totalDocsExamined":0,
      "executionStages":{  
         "stage":"FETCH",
         "nReturned":0,
         "executionTimeMillisEstimate":0,
         "works":1,
         "advanced":0,
         "needTime":0,
         "needFetch":0,
         "saveState":0,
         "restoreState":0,
         "isEOF":1,
         "invalidates":0,
         "docsExamined":0,
         "alreadyHasObj":0,
         "inputStage":{  
            "stage":"IXSCAN",
            "nReturned":0,
            "executionTimeMillisEstimate":0,
            "works":1,
            "advanced":0,
            "needTime":0,
            "needFetch":0,
            "saveState":0,
            "restoreState":0,
            "isEOF":1,
            "invalidates":0,
            "keyPattern":{  
               "code":1
            },
            "indexName":"userid_1",
            "isMultiKey":false,
            "direction":"forward",
            "indexBounds":{  
               "code":[  
                  "[\"1111\", \"1111\"]"
               ]
            },
            "keysExamined":0,
            "dupsTested":0,
            "dupsDropped":0,
            "seenInvalidated":0,
            "matchTested":0
         }
      },
      "allPlansExecution":[  

      ]
   },

1 个答案:

答案 0 :(得分:1)

如果按索引只有一个匹配,则defenitely得到totalKeysExamined = 1和totalDocsExamined = 1。

  • 检查您是否使用了正确的集合:db.getCollection(&#34; indexdemo &#34;) vs db。 indextest .find()
  • 确保db.indextest.find({code:5000})返回1个文档。到目前为止,你已经nReturned = 0。
  • 检查索引名称:通常如果您创建索引{code:1},它将被命名为code_1,您的统计信息显示&#34; indexName&#34;:&#34; userid_1&#34;