我试图编写一个 python 脚本来将数据加载到 mongodb 但它失败了。我正在使用 mongodb 地图集。我正在从 ubuntu 执行脚本。它能够建立连接。但永远不会看到正在创建的数据库或集合。
我写的代码:
from pymongo import MongoClient
import urllib
import sys
import pandas as pd
import pymongo
import json
import os
def import_content(filepath):
mng_client = pymongo.MongoClient("mongodb://sathish1000:siva123.A@cluster0-shard-00-00-vgsfk.mongodb.net:27017/<DATABASE>?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin")
mng_db = mng_client['local']
collection_name = 'collection_name'
db_cm = mng_db[collection_name]
print(db_cm)
cdir = os.path.dirname(__file__)
file_res = os.path.join(cdir, filepath)
data = pd.read_csv(file_res)
data_json = json.loads(data.to_json(orient='records'))
print(db_cm.find() )
i = 0
for data in data_json:
i = i+1
print(i)
business = {"value":i}
db_cm.insert_one(data)
if __name__ == "__main__":
filepath = '/home/sathish/Downloads/Train.csv'
import_content(filepath)
我得到的错误如下:
Traceback (most recent call last):
File "LoadMongo.py", line 32, in <module>
import_content(filepath)
File "LoadMongo.py", line 28, in import_content
db_cm.insert_one(data)
File "/usr/local/lib/python3.5/dist-packages/pymongo/collection.py", line 654, in insert_one
with self._socket_for_writes() as sock_info:
File "/usr/lib/python3.5/contextlib.py", line 59, in __enter__
return next(self.gen)
File "/usr/local/lib/python3.5/dist-packages/pymongo/mongo_client.py", line 825, in _get_socket
with server.get_socket(self.__all_credentials) as sock_info:
File "/usr/lib/python3.5/contextlib.py", line 59, in __enter__
return next(self.gen)
File "/usr/local/lib/python3.5/dist-packages/pymongo/server.py", line 168, in get_socket
with self.pool.get_socket(all_credentials, checkout) as sock_info:
File "/usr/lib/python3.5/contextlib.py", line 59, in __enter__
return next(self.gen)
File "/usr/local/lib/python3.5/dist-packages/pymongo/pool.py", line 792, in get_socket
sock_info.check_auth(all_credentials)
File "/usr/local/lib/python3.5/dist-packages/pymongo/pool.py", line 512, in check_auth
auth.authenticate(credentials, self)
File "/usr/local/lib/python3.5/dist-packages/pymongo/auth.py", line 470, in authenticate
auth_func(credentials, sock_info)
File "/usr/local/lib/python3.5/dist-packages/pymongo/auth.py", line 450, in _authenticate_default
return _authenticate_scram_sha1(credentials, sock_info)
File "/usr/local/lib/python3.5/dist-packages/pymongo/auth.py", line 229, in _authenticate_scram_sha1
res = sock_info.command(source, cmd)
File "/usr/local/lib/python3.5/dist-packages/pymongo/pool.py", line 424, in command
self._raise_connection_failure(error)
File "/usr/local/lib/python3.5/dist-packages/pymongo/pool.py", line 552, in _raise_connection_failure
raise error
File "/usr/local/lib/python3.5/dist-packages/pymongo/pool.py", line 419, in command
collation=collation)
File "/usr/local/lib/python3.5/dist-packages/pymongo/network.py", line 110, in command
response, codec_options=codec_options)
File "/usr/local/lib/python3.5/dist-packages/pymongo/helpers.py", line 128, in _unpack_response
if error_object["$err"].startswith("not master"):
KeyError: '$err'
有人可以帮我弄这个吗 ?