Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从mongodb atlas python读取数据_Python_Python 3.x_Mongodb_Azure_Pymongo - Fatal编程技术网

从mongodb atlas python读取数据

从mongodb atlas python读取数据,python,python-3.x,mongodb,azure,pymongo,Python,Python 3.x,Mongodb,Azure,Pymongo,我试图从python语言读取mongoDB中的数据。我尝试过不同的代码,但都不起作用 from pymongo import MongoClient from pprint import pprint client = MongoClient("mongodb+srv://username:password@cluster1-hc9jb.azure.mongodb.net/test?retryWrites=true&w=majority") db = client.sample_ai

我试图从python语言读取mongoDB中的数据。我尝试过不同的代码,但都不起作用


from pymongo import MongoClient
from pprint import pprint

client = MongoClient("mongodb+srv://username:password@cluster1-hc9jb.azure.mongodb.net/test?retryWrites=true&w=majority")
db = client.sample_airbnb
collection = db.listingsAndReviews.find()
for inventory in collection:
     pprint(inventory)
得到了这个错误

runfile('C:/Users/Umer MiB/.spyder-py3/temp.py', wdir='C:/Users/Umer MiB/.spyder-py3')
Traceback (most recent call last):

  File "<ipython-input-9-58c4aa2d2ef7>", line 1, in <module>
    runfile('C:/Users/Umer MiB/.spyder-py3/temp.py', wdir='C:/Users/Umer MiB/.spyder-py3')

  File "D:\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "D:\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/Umer MiB/.spyder-py3/temp.py", line 19, in <module>
    for inventory in collection:

  File "D:\Anaconda3\lib\site-packages\pymongo\cursor.py", line 1156, in next
    if len(self.__data) or self._refresh():

  File "D:\Anaconda3\lib\site-packages\pymongo\cursor.py", line 1050, in _refresh
    self.__session = self.__collection.database.client._ensure_session()

  File "D:\Anaconda3\lib\site-packages\pymongo\mongo_client.py", line 1810, in _ensure_session
    return self.__start_session(True, causal_consistency=False)

  File "D:\Anaconda3\lib\site-packages\pymongo\mongo_client.py", line 1763, in __start_session
    server_session = self._get_server_session()

  File "D:\Anaconda3\lib\site-packages\pymongo\mongo_client.py", line 1796, in _get_server_session
    return self._topology.get_server_session()

  File "D:\Anaconda3\lib\site-packages\pymongo\topology.py", line 490, in get_server_session
    None)

  File "D:\Anaconda3\lib\site-packages\pymongo\topology.py", line 209, in _select_servers_loop
    self._error_message(selector))

ServerSelectionTimeoutError: connection closed,connection closed,connection closed



runfile('C:/Users/Umer-MiB/.spyder-py3/temp.py',wdir='C:/Users/Umer-MiB/.spyder-py3')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
运行文件('C:/Users/Umer-MiB/.spyder-py3/temp.py',wdir='C:/Users/Umer-MiB/.spyder-py3')
文件“D:\Anaconda3\lib\site packages\spyder\u kernels\customize\spyderrcustomize.py”,第827行,在runfile中
execfile(文件名、命名空间)
文件“D:\Anaconda3\lib\site packages\spyder\u kernels\customize\spydercustomize.py”,第110行,在execfile中
exec(编译(f.read(),文件名,'exec'),命名空间)
文件“C:/Users/Umer MiB/.spyder-py3/temp.py”,第19行,在
对于收款中的存货:
文件“D:\Anaconda3\lib\site packages\pymongo\cursor.py”,第1156行,下一页
如果len(self.\u数据)或self.\u refresh():
文件“D:\Anaconda3\lib\site packages\pymongo\cursor.py”,第1050行,在\u刷新中
self.\u session=self.\u collection.database.client.\u session()
文件“D:\Anaconda3\lib\site packages\pymongo\mongo\u client.py”,第1810行,在会话中
返回self.\u启动\u会话(True,因果一致性=False)
文件“D:\Anaconda3\lib\site packages\pymongo\mongo\u client.py”,第1763行,在启动会话中
server\u session=self.\u get\u server\u session()
文件“D:\Anaconda3\lib\site packages\pymongo\mongo\u client.py”,第1796行,在\u get\u server\u会话中
返回self.\u拓扑。获取\u服务器\u会话()
文件“D:\Anaconda3\lib\site packages\pymongo\topology.py”,第490行,在get_服务器_会话中
(无)
文件“D:\Anaconda3\lib\site packages\pymongo\topology.py”,第209行,在\u select\u servers\u循环中
自身错误消息(选择器))
ServerSelectionTimeoutError:连接已关闭、连接已关闭、连接已关闭
我遵循了描述
https://stackoverflow.com/questions/57343409/how-do-i-retrieve-and-print-pymongo-cursor-cursor-objects
但没有一个有效
我也遵循了
https://docs.mongodb.com/guides/server/read/
但读取失败

请尝试以下代码,但首先进行替换:将\u this\u替换为\u用户名,将\u this\u替换为\u密码,将\u this\u替换为\u数据库\u名称,将\u this\u替换为\u收藏\u名称。 此外,在做任何事情之前,只需创建一个具有非常简单密码的新用户(没有特殊字符,因为您需要对其进行编码),并尝试使用新用户访问您的数据库。更多信息请参见

也许您还需要安装一些软件包,如:

pip install pymongo[srv]

感谢您提供的上述信息,在我这样做之后,它为我发挥了作用:

pip安装dnspython

并更改了两行:

client = MongoClient("replace with MONGODB_URI")
db = client['replace_this_with_database_name']

在迁移到MongodBattlas之后,上述内容对于我的Nightscout数据是必要的。

您的代码适用于python 3.7、pymongo 3.7.0和dnspython 1.16.0。请在MongoDB Atlas中检查“安全性”>“数据库访问”菜单中的用户和密码是否正确,并在“安全性”>“网络访问”菜单中检查您的IP地址是否正确列入白名单?如果这是正确的白名单,您应该能够ping以下3个节点中的任何一个:
ping:cluster0-shard-00-00-abcde.mongodb.net
client = MongoClient("replace with MONGODB_URI")
db = client['replace_this_with_database_name']