I am very new to mongo db. Trying do simple mongo db extraction operation using find method on localhost only. Mongo is running on all default settings. Using python 3.7 and pymongo version 3.11.3 package. I am not getting what is exactly going wrong with me. Sharing the code as below.

Scenario #1

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)

which gives me below output

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')
<pymongo.cursor.Cursor object at 0x000001D368534160>
Traceback (most recent call last):
  File "C:/Users/PycharmProjects/flask_app_stuff/1_ mongo_restful/mongodb_connection.py", line 11, in <module>
    for i in found:
  File "C:\Users\PycharmProjects\flask_app_stuff\.venv\lib\site-packages\pymongo\cursor.py", line 1207, in next
    if len(self.__data) or self._refresh():
  File "C:\Users\PycharmProjects\flask_app_stuff\.venv\lib\site-packages\pymongo\cursor.py", line 1100, in _refresh
    self.__session = self.__collection.database.client._ensure_session()
  File "C:\Users\PycharmProjects\flask_app_stuff\.venv\lib\site-packages\pymongo\mongo_client.py", line 1816, in _ensure_session
    return self.__start_session(True, causal_consistency=False)
  File "C:\Users\PycharmProjects\flask_app_stuff\.venv\lib\site-packages\pymongo\mongo_client.py", line 1766, in __start_session
    server_session = self._get_server_session()
  File "C:\Users\PycharmProjects\flask_app_stuff\.venv\lib\site-packages\pymongo\mongo_client.py", line 1802, in _get_server_session
    return self._topology.get_server_session()
  File "C:\Users\PycharmProjects\flask_app_stuff\.venv\lib\site-packages\pymongo\topology.py", line 488, in get_server_session
    None)
  File "C:\Users\PycharmProjects\flask_app_stuff\.venv\lib\site-packages\pymongo\topology.py", 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: <TopologyDescription id: 60804696a4bda0d1ee4db3f9, topology_type: Single, servers: [<ServerDescription ('localhost', 27017) server_type: Unknown, rtt: None, error=InvalidBSON('hour must be in 0..23')>]>

Process finished with exit code 1

My actual goal is to build flask version 1.1.2 API where I am using Flask-PyMongo version 2.3.0 but that also giving me same error, so I tried to build basic one first. code is as below

Scenario #2

from flask import Flask, render_template
from flask_pymongo import PyMongo


app = Flask(__name__)
app.config["MONGO_URI"] = "mongodb://localhost:27017/mydb"
mongo = PyMongo(app)
print(mongo.db)


@app.route("/")
def home_page():
    online_users = mongo.db.VA_collection.find_one({"online": True})
    for k in online_users:
        print(k)
    return render_template("index.html", online_users=online_users)


if __name__ == "__main__":
    app.run(debug=True)

error log as below

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: <TopologyDescription id: 6080490c1fe900cc22d6b873, topology_type: Single, servers: [<ServerDescription ('localhost', 27017) server_type: Unknown, rtt: None, error=InvalidBSON('hour must be in 0..23')>]>

scenario #1 Simple MongoClient What is my observation is when I tried to connect db with simple mongoclient constructor gives me this output Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True) where connect is True

scenario #2 Flask app When I used flask-pymongo its database connection constructor gives Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=False), 'mydb') where connect is Flase

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

1

There are 1 best solutions below

1
On

May be you can try using something like this:

from pymongo import MongoClient    
mongo = MongoClient('mongodb://localhost:27017')['mydb']

instead of

app.config["MONGO_URI"] = "mongodb://localhost:27017/mydb"
mongo = PyMongo(app)