People will offer you many tolls , and they will claims that their tools are very much proficient in dealing with migration work from Mongodb to MySQL , But I wanted to tell you it is not at all easy task.If your database having too much foreign and primary key kind of concept than it will be like hardest work . So you must analyse once before going to take final decisions . I can suggest two methods .
above is a simple example which will convert random string id's into a numeric id's,
- Convert your MySQL table data into CSV format , and this can be easily achieved by using mysql export table , you may face problem if your table is too large , you can export in chunks also , like export from 1 to 1000 again from 1001 to 2000 in the same way you. Now you can import these CSV files into mongodb using mongo Import tools . But these process is little risky , there are huge change to lost your precious data .For example suppose you have a table called employee and a another table called department tabele . If both employee and department table have foreign and primary key , than it will be a big challenge , Because in CSV which we exported from MySQL does not contains information about relations of these two tables . And we know mongoDb generate it's own auto increment id's . So it will be very hard to map two collection with the name employee and department after importing CSV .
- Second way is to create a script which map fetch data from MySQL and insert into Mongodb collections one by one . I am not telling this is the best to do , it is safer as comparing to CSV import into mongoDb collections. we can also generate Auto increment id' using mongodb function called "findAndModify" below is way to do so .
db.sequence.findAndModify({
query: {"_id": "user"},
update : {$inc : {"seq":1}},
upsert:true,
new:true})
above is a simple example which will convert random string id's into a numeric id's,
- Finds (or creates) the “sequence” collection
- Gets the document with id “user”
- Increments the value of “seq” by 1 ,so basically increasing our auto increment by value one .
- If this document doesn’t exist, it creates it (upsert:true)
- Returns the new value of “seq”
No comments:
Post a Comment