title: Spring Data MongoDB Slow MongoTemplate.find() Performance
body: <p>I'm having performance issues when querying ~12,000 user documents, indexed by 1 column, (companyId), no other filter. The whole collection only has ~27000.  It took me about 12 seconds to get the ~12000 rows of data...</p>
  
  <p>I tried running explain for this query:
      db.instoreMember.find({companyId:"5b6be3e2096abd567974f924"}).explain();</p>
  
  <p>result follows:</p>
  
  <pre><code>{
      "queryPlanner" : {
          "plannerVersion" : 1,
          "namespace" : "production.instoreMember",
          "indexFilterSet" : false,
          "parsedQuery" : {
              "companyId" : {
                  "$eq" : "5b6be3e2096abd567974f924"
              }
          },
          "winningPlan" : {
              "stage" : "FETCH",
              "inputStage" : {
                  "stage" : "IXSCAN",
                  "keyPattern" : {
                      "companyId" : 1,
                      "name" : 1,
                      "phoneNumber" : 1
                  },
                  "indexName" : "companyId_1_name_1_phoneNumber_1",
                  "isMultiKey" : false,
                  "multiKeyPaths" : {
                      "companyId" : [ ],
                      "name" : [ ],
                      "phoneNumber" : [ ]
                  },
                  "isUnique" : true,
                  "isSparse" : false,
                  "isPartial" : false,
                  "indexVersion" : 2,
                  "direction" : "forward",
                  "indexBounds" : {
                      "companyId" : [
                          "[\"5b6be3e2096abd567974f924\", \"5b6be3e2096abd567974f924\"]"
                      ],
                      "name" : [
                          "[MinKey, MaxKey]"
                      ],
                      "phoneNumber" : [
                          "[MinKey, MaxKey]"
                      ]
                  }
              }
          },
          "rejectedPlans" : [
              {
                  "stage" : "FETCH",
                  "inputStage" : {
                      "stage" : "IXSCAN",
                      "keyPattern" : {
                          "companyId" : 1
                      },
                      "indexName" : "companyId_1",
                      "isMultiKey" : false,
                      "multiKeyPaths" : {
                          "companyId" : [ ]
                      },
                      "isUnique" : false,
                      "isSparse" : false,
                      "isPartial" : false,
                      "indexVersion" : 2,
                      "direction" : "forward",
                      "indexBounds" : {
                          "companyId" : [
                              "[\"5b6be3e2096abd567974f924\", \"5b6be3e2096abd567974f924\"]"
                          ]
                      }
                  }
              }
          ]
      },
      "serverInfo" : {
  
      },
      "ok" : 1
  }
  </code></pre>
  
  <p>It seems that it is actually using the indexed companyId field, and if i do the search directly via mongodb shell, it's very fast: only 1~2 seconds.</p>
  
  <p>But via Spring MongoDB Data - MongoTemplate:</p>
  
  <pre><code>final Query query = new Query().addCriteria(Criteria.where("companyId").is(adminCompanyId));
  final List&lt;InstoreMember&gt; listOfInstoreMembers = mongoTemplate.find(query, InstoreMember.class);
  </code></pre>
  
  <p>This becomes very slow ~10-12seconds. (How i measure is that I put a break point at the find statement, let it step through to next line, which took about ~10-12seconds)</p>
  
  <p>I've added the DEBUG line for mongodb spring bootstrap and here is the logged output of the find statement :</p>
  
  <pre><code>2018-08-14 23:53:34.493 DEBUG 22733 --- [bio-8080-exec-2] o.s.data.mongodb.core.MongoTemplate      : 
  find using query: { "companyId" : "58fa36dd31d103038e64b061"} fields: null for class: class fn.model.InstoreMember in collection: instoreMember
  </code></pre>
  
  <p>Version of spring-data-mongodb i use:     </p>
  
  <pre><code>compile ("org.springframework.data:spring-data-mongodb:1.10.7.RELEASE")
  </code></pre>