title: Mongo find query takes 2 minutes
body: <p>I have around 75,000 documents in a collection.</p>
  
  <p>The total size of database is around 45GB.<br>
  Of the 75k docs, around 45k are of 900 KB each (around 42 GB) and the remaining docs around 120 KB each.</p>
  
  <p>Each document is mapped to a <code>custId</code> ObjectId in other collection, and has a <code>timestamp</code>, both indexed.</p>
  
  <p>Now I need to fetch the docs for a particular <code>custId</code> for last month.
  The count is around 5500 docs. This <code>custId</code> has the small docs with size around 120 KB each.</p>
  
  <p><strong>Following is my query:</strong></p>
  
  <pre><code>db.mycollection.find(
  {
      custId:ObjectId("CUST_OBJECT_ID_HERE"),
      timestamp:{$gte:one_month_ago_date, $lt:current_date}
  }).sort({timestamp:-1})
  </code></pre>
  
  <p>Still the query takes 2 minutes to fetch all records. Is it because of the number of documents or the size of the larger documents? Is there a way I can fix this?</p>
  
  <p><strong>NOTE:</strong>
  It takes 2 minutes when the query is fired from nodejs. If I fire it on mongo shell, it returns quickly but it might be because its just fetching first 50 records. When I attached <code>.count()</code> to the query on mongo shell, it took 2 minutes to return with the count. </p>
  
  <p><strong>Update:</strong><br>
  <strong>Indexing details:</strong>  </p>
  
  <pre><code>"wiredTiger" : {
      "nindexes" : 3,
      "totalIndexSize" : 2396160,
      "indexSizes" : {
          "_id_" : 1138688,
          "custId_1" : 598016,
          "timestamp_1" : 659456
      }
  }
  </code></pre>
  
  <p><strong>Explain Output: (with sort)</strong></p>
  
  <pre><code>{
      "queryPlanner" : {
          "plannerVersion" : 1,
          "namespace" : "mydb.mycollection",
          "indexFilterSet" : false,
          "parsedQuery" : {
              "$and" : [
                  {
                      "custId" : {
                          "$eq" : ObjectId("CUST_OBJECT_ID_HERE")
                      }
                  },
                  {
                      "timestamp" : {
                          "$lt" : ISODate("2017-05-15T14:20:04.393Z")
                      }
                  },
                  {
                      "timestamp" : {
                          "$gte" : ISODate("2017-04-15T14:20:04.393Z")
                      }
                  }
              ]
          },
          "winningPlan" : {
              "stage" : "FETCH",
              "filter" : {
                  "custId" : {
                      "$eq" : ObjectId("CUST_OBJECT_ID_HERE")
                  }
              },
              "inputStage" : {
                  "stage" : "IXSCAN",
                  "keyPattern" : {
                      "timestamp" : 1
                  },
                  "indexName" : "timestamp_1",
                  "isMultiKey" : false,
                  "isUnique" : false,
                  "isSparse" : false,
                  "isPartial" : false,
                  "indexVersion" : 1,
                  "direction" : "backward",
                  "indexBounds" : {
                      "timestamp" : [
                          "(new Date(1494858004393), new Date(1492266004393)]"
                      ]
                  }
              }
          },
          "rejectedPlans" : [
              {
                  "stage" : "SORT",
                  "sortPattern" : {
                      "timestamp" : -1
                  },
                  "inputStage" : {
                      "stage" : "SORT_KEY_GENERATOR",
                      "inputStage" : {
                          "stage" : "FETCH",
                          "filter" : {
                              "$and" : [
                                  {
                                      "timestamp" : {
                                          "$lt" : ISODate("2017-05-15T14:20:04.393Z")
                                      }
                                  },
                                  {
                                      "timestamp" : {
                                          "$gte" : ISODate("2017-04-15T14:20:04.393Z")
                                      }
                                  }
                              ]
                          },
                          "inputStage" : {
                              "stage" : "IXSCAN",
                              "keyPattern" : {
                                  "custId" : 1
                              },
                              "indexName" : "custId_1",
                              "isMultiKey" : false,
                              "isUnique" : false,
                              "isSparse" : false,
                              "isPartial" : false,
                              "indexVersion" : 1,
                              "direction" : "forward",
                              "indexBounds" : {
                                  "custId" : [
                                      "[ObjectId('CUST_OBJECT_ID_HERE'), ObjectId('CUST_OBJECT_ID_HERE')]"
                                  ]
                              }
                          }
                      }
                  }
              }
          ]
      },
      "serverInfo" : {
          "host" : "test-machine",
          "port" : 27017,
          "version" : "3.2.12",
          "gitVersion" : "REMOVED_BY_OP"
      },
      "ok" : 1
  }
  </code></pre>