0

我对 mongo db 很陌生。尝试仅在本地主机上使用 find 方法进行简单的mongo db 提取操作。Mongo 在所有默认设置下运行。使用python 3.7pymongo version 3.11.3包装。我不明白我到底出了什么问题。共享代码如下。

情景#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)

这给了我下面的输出

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

flask version 1.1.2我的实际目标是在我使用的地方构建API,Flask-PyMongo version 2.3.0但这也给了我同样的错误,所以我尝试先构建基本的 API。代码如下

情景#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)

错误日志如下

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')>]>

场景#1 简单的 MongoClient 我的观察是当我尝试使用简单的 mongoclient 构造函数连接 db 时给我这个输出Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True),其中connect 为 True

场景#2 Flask 应用程序 当我使用 flask-pymongo 时,它的数据库连接构造函数给出 了 connect 是 Fl​​ase 的位置Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=False), 'mydb')

我不知道它的含义是什么,但认为它可能会带来一些帮助。提前致谢,任何帮助将不胜感激。如果需要任何额外的细节,请告诉我

4

0 回答 0