12/10/2021

mongodb python, Table recode export, loading, save csv, load csv

 refer to below code:


.

#connet db
client = pymongo.MongoClient("mongodb://localhost:27017/")
#connet table
who_table = db["who_table"]

#delete all
who_table.drop()

#load csv to table
df = pd.read_csv('1.csv')
data = df.to_dict('records')
who_table.insert_many(data)
#show all items
for x in who_table.find():
print(x)

#save table to csv
cursor = who_table.find({})
df = pd.DataFrame(list(cursor))
df.to_csv('2.csv', index=False)

.


Thank you.๐Ÿ™‡๐Ÿป‍♂️

www.marearts.com