title: pymongo pymongo.errors.ServerSelectionTimeoutError:hour must be in 0..23, Timeout: 30s, Topology Description:
body: <p>I am very new to mongo db. Trying do simple <strong>mongo</strong> db extraction operation using find method on localhost only. Mongo is running on all <strong>default settings</strong>. Using <strong><code>python 3.7</code></strong> and <strong><code>pymongo version 3.11.3</code></strong> package. I am not getting what is exactly going wrong with me. Sharing the code as below.</p>
  <p><strong>Scenario #1</strong></p>
  <pre><code>from pymongo import MongoClient
  con = MongoClient()
  # con = MongoClient(host='localhost', port=27017) #tried this way also
  print(con)
  db = con.mydb
  print(db)
  collection = db.VA_collection
  print(collection)
  found = collection.find({'online':True})  # find returns all entries
  print(found)
  for i in found:
      print(i)
  </code></pre>
  <p>which gives me below output</p>
  <pre><code>MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True)
  Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'mydb')
  Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'mydb'), 'VA_collection')
  &lt;pymongo.cursor.Cursor object at 0x000001D368534160&gt;
  Traceback (most recent call last):
    File &quot;C:/Users/PycharmProjects/flask_app_stuff/1_ mongo_restful/mongodb_connection.py&quot;, line 11, in &lt;module&gt;
      for i in found:
    File &quot;C:\Users\PycharmProjects\flask_app_stuff\.venv\lib\site-packages\pymongo\cursor.py&quot;, line 1207, in next
      if len(self.__data) or self._refresh():
    File &quot;C:\Users\PycharmProjects\flask_app_stuff\.venv\lib\site-packages\pymongo\cursor.py&quot;, line 1100, in _refresh
      self.__session = self.__collection.database.client._ensure_session()
    File &quot;C:\Users\PycharmProjects\flask_app_stuff\.venv\lib\site-packages\pymongo\mongo_client.py&quot;, line 1816, in _ensure_session
      return self.__start_session(True, causal_consistency=False)
    File &quot;C:\Users\PycharmProjects\flask_app_stuff\.venv\lib\site-packages\pymongo\mongo_client.py&quot;, line 1766, in __start_session
      server_session = self._get_server_session()
    File &quot;C:\Users\PycharmProjects\flask_app_stuff\.venv\lib\site-packages\pymongo\mongo_client.py&quot;, line 1802, in _get_server_session
      return self._topology.get_server_session()
    File &quot;C:\Users\PycharmProjects\flask_app_stuff\.venv\lib\site-packages\pymongo\topology.py&quot;, line 488, in get_server_session
      None)
    File &quot;C:\Users\PycharmProjects\flask_app_stuff\.venv\lib\site-packages\pymongo\topology.py&quot;, line 217, in _select_servers_loop
      (self._error_message(selector), timeout, self.description))
  pymongo.errors.ServerSelectionTimeoutError: hour must be in 0..23, Timeout: 30s, Topology Description: &lt;TopologyDescription id: 60804696a4bda0d1ee4db3f9, topology_type: Single, servers: [&lt;ServerDescription ('localhost', 27017) server_type: Unknown, rtt: None, error=InvalidBSON('hour must be in 0..23')&gt;]&gt;
  
  Process finished with exit code 1
  </code></pre>
  <p>My actual goal is to build <code>flask version 1.1.2</code> API where I am using <code>Flask-PyMongo version 2.3.0</code> but that also giving me same error, so I tried to build basic one first. code is as below</p>
  <p><strong>Scenario #2</strong></p>
  <pre><code>from flask import Flask, render_template
  from flask_pymongo import PyMongo
  
  
  app = Flask(__name__)
  app.config[&quot;MONGO_URI&quot;] = &quot;mongodb://localhost:27017/mydb&quot;
  mongo = PyMongo(app)
  print(mongo.db)
  
  
  @app.route(&quot;/&quot;)
  def home_page():
      online_users = mongo.db.VA_collection.find_one({&quot;online&quot;: True})
      for k in online_users:
          print(k)
      return render_template(&quot;index.html&quot;, online_users=online_users)
  
  
  if __name__ == &quot;__main__&quot;:
      app.run(debug=True)
  </code></pre>
  <p>error log as below</p>
  <pre><code>Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=False), 'mydb')
  pymongo.errors.ServerSelectionTimeoutError: hour must be in 0..23, Timeout: 30s, Topology Description: &lt;TopologyDescription id: 6080490c1fe900cc22d6b873, topology_type: Single, servers: [&lt;ServerDescription ('localhost', 27017) server_type: Unknown, rtt: None, error=InvalidBSON('hour must be in 0..23')&gt;]&gt;
  </code></pre>
  <p><strong>scenario #1 Simple MongoClient</strong>
  What is my observation is when I tried to connect db with simple mongoclient constructor gives me this  output <strong><code>Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True)</code></strong> where <strong>connect is True</strong></p>
  <p><strong>scenario #2 Flask app</strong>
  When I used flask-pymongo its database connection constructor gives  <strong><code>Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=False), 'mydb')</code></strong> <strong>where connect is Flase</strong></p>
  <p>I do not what is it's meaning but thought It might bring some help. Thanks in advance and any Help will appreciate. Please let me know if any extra details is required</p>