title: Querying database for documents within a specified date range using FastAPI and MongoDB
body: <p>I have a collection in my MongoDB with records that have an attribute &quot;timestamp&quot; which is stored as type <code>datetime</code></p>
  <p>I want to use FastAPI to query and return all records which are within a given range of dates that I am passing on as query parameters.</p>
  <pre><code>db = client[&quot;tasks&quot;]
  
  async def list_activities(
      lower_date: str = &quot;&quot;, upper_date: str = &quot;&quot;
  ):
      start_date = datetime.strptime(lower_date, &quot;%d-%m-%Y&quot;)
      end_date = datetime.strptime(upper_date, &quot;%d-%m-%Y&quot;)
      activities = (
          await db[&quot;activities&quot;]
          .find({&quot;timestamp&quot;: {&quot;$gte&quot;: start_date, &quot;$lt&quot;: end_date}})
          .to_list(1000)
      )
      return activities
  </code></pre>
  <p>On running this with some parameters for lower and upper dates, I'm being returned an empty list</p>
  <p>However I wrote another program to test this</p>
  <pre><code>MONGODB_URL = os.getenv(&quot;MONGODB_URL&quot;)
  mc = pymongo.MongoClient(MONGODB_URL)
  tgdb = mc[&quot;tasks&quot;]
  tgcol = tgdb[&quot;activities&quot;]
  recordno = 1
  
  for x in tgcol.find(
      {&quot;timestamp&quot;: {&quot;$gte&quot;: &quot;2022-01-31 00:00:00&quot;, &quot;$lt&quot;: &quot;2022-02-06 00:00:00&quot;}}
  ):
      print(&quot;Record No: &quot; + str(recordno))
      recordno += 1
      print(x)
  </code></pre>
  <p>This on the other hand returns all the records within the date range just fine.</p>
  <p>Any clue as to what I'm doing wrong with the first piece of code?</p>