title: execution time of mongodb find query
body: <p>I am trying to benchmark mongodb performance and I am having problems understanding how mongodb executes queries, specifically how long they take to complete.</p>
  
  <p>If I run the following code:</p>
  
  <pre><code>import pymongo
  #Connect to the database
  client = MongoClient("mongodb://.../testrecords")
  db = client.testrecords
  
  start = datetime.datetime.now()
  
  result = db.threads.find( {"$and": [{ "location" : "JC018" }, {"timestamp": "2018-03-22T23:05:15+00:00"}  ] } ).explain()
  
  endtime = datetime.datetime.now()
  print ("duration: " + str(endtime-start))
  print(result)
  </code></pre>
  
  <p>I receive following output: <code>duration: 0:00:00.531754</code>. I also get the results of the <code>explanation()</code> function providing the following information <code>executionTimeMillis': 249</code></p>
  
  <p>This makes sense as the time taken by mongodb to execute the query is less than the roundtrip time.</p>
  
  <p>However if I use the following loop to run the same query 10,000 times, the execution duration is consistently recorded as between 200 and 300 milliseconds. (Note that I have removed the <code>explain()</code> call.)
  I fail to see how running the query 10,000 times can result in no meaningful increase in execution time.</p>
  
  <pre><code>for i in range(10000):
      result = db.threads.find( {"$and": [{ "location" : "JC018" }, {"timestamp": "2018-03-22T23:05:15+00:00"}  ] } )
  </code></pre>
  
  <p>However, if I run the loop with the <code>explain()</code> function it does appear to take approximately n * 250ms to execute the loop. </p>
  
  <pre><code>for i in range(n):
      result = db.threads.find( {"$and": [{ "location" : "JC018" }, {"timestamp": "2018-03-22T23:05:15+00:00"}  ] } )
  </code></pre>
  
  <p>Can anyone explain the lack of a time difference in executing the query once and executing it 10,000 times and why adding the explain() function to the loop appears to result in the expected execution time?</p>
  
  <p>I thought that there may be some kind of caching going on but I am only using PyMongo on the client side and cannot find any mention of this in the documentation.</p>
  
  <p>Thanks</p>