简体   繁体   中英

'MongoClient' object is not iterable error

I'm trying to upload a large json file into MongoDB Atlas in python3. Here is my code

def connectMongoDb():
    username = urllib.parse.quote_plus("test@gmail.com")
    password =urllib.parse.quote_plus("4-mmmm!")



   try:
       url = pymongo.MongoClient("mongodb+srv://"+username+":"+password+"@realmcluster.vlout.mongodb.net/<dbname>?retryWrites=true&w=majority")
     

     cluster= MongoClient(url)
     db = cluster["store"]
     collections = ["items"]

except Exception as e:
     print("connection error ", str(e))
 try:
    with open('storeItems.json') as f:
        file_data = json.load(f)

     
        collections.insert_one(file_data)
        print(collections)

except Exception as e:
    print("writting error", str(e)) 

the error i keep getting are

'MongoClient' object is not iterable Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

how do i fix this problem

You have already initialized the MongoClient constructor in the line url = pymongo.MongoClient("mongodb+srv://"+username+":"+password+"@realmcluster.vlout.mongodb.net/<dbname>?retryWrites=true&w=majority") and so you don't need to do cluster= MongoClient(url) . Also it seems that in order to get a collection you need to do collections = db["items"]

Read the documentation for additional help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM