Skip to content

Instantly share code, notes, and snippets.

@jrgavilanes
Created January 19, 2019 18:41
Show Gist options
  • Save jrgavilanes/0335374b6aacd56398dc12bfa438e5b8 to your computer and use it in GitHub Desktop.
Save jrgavilanes/0335374b6aacd56398dc12bfa438e5b8 to your computer and use it in GitHub Desktop.
Example how to migrate a table from a mySql database to a mongoDB
import pymysql
import pymongo
# Set mongo connection
connection = pymongo.MongoClient(host_mongo, port_mongo)
db = connection[bd_mongo]
db.authenticate(user_mongo, pass_mongo)
notes = db.collection_name
# Set mysql connection
conn = pymysql.connect(host='127.0.0.1', user=user_mysql, passwd=pass_mysql, db=db_mysql)
cur = conn.cursor()
cur.execute("select * from table order by id")
for r in cur:
# f = open(str(r[0])+"-"+r[1]+".txt", "w+")
# f.write(r[3])
# f.close()
note = {"title": r[1], "content": r[3]}
print(notes.insert_one(note).inserted_id)
# Close connections
cur.close()
conn.close()
connection.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment