title: Getting count of number of elements in mongodb when documents are less than $limit (aggregation)
body: <p>I cold not find anywhere that what happens when we limit the number of elements to <code>N</code> and the actual documents prsent according to filter are <code>k</code> where <code>k&gt;N</code>. My common sense says that developers have take care of the this via <code>minimum (N,actual_found)</code> but I could not find a solution.</p>
  <p>I have a query in <code>pymongo</code> where I want to find the number of documents according to the filter.
  I have a query something like that:</p>
  <pre><code>query = [{&quot;$match&quot;: {&quot;$expr&quot;: {&quot;$and&quot;: [{&quot;$lte&quot;: [{&quot;$strLenCP&quot;: {&quot;$ifNull&quot;: [&quot;$eng_text&quot;,&quot;&quot;]}},0]},
                                          {&quot;$lte&quot;: [{&quot;$strLenCP&quot;: {&quot;$ifNull&quot;: [&quot;$hindi_text&quot;,&quot;&quot;]}},0]},
                                          {&quot;$lte&quot;: [{&quot;$strLenCP&quot;: {&quot;$ifNull&quot;: [&quot;$latex&quot;,&quot;&quot;]}},0]}]}}},
           {&quot;$limit&quot;: 1000000}]
  
  objs =  db.questions.aggregate(query)
  
  </code></pre>
  <p>The problem is that when I ran</p>
  <pre><code>count=0
  for obj in objs:
      count+=1
  print(count)
  &gt;&gt;&gt;
  320953
  </code></pre>
  <p>It gave me the results but a lot of time was taken as it had to go to server to fetch every query and then a loop.</p>
  <p>Is there any method where I can find the count of results?</p>
  <p>I tried <code>db.questions.count_documents(query)</code> and <code>db.documents.count_documents({query})</code>
  and it returned me an error. MAybe because these are aggregating thing or for whatever reason. I am not a DB guy but just want to see a logic to get the data. I think I can reshape the query and then use <code>count_documents()</code>. Could someone please tell me a way to do that?</p>