Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrating from MySQL to MongoDB - best practices

So, it may be best to just try it out and see through some trial and error, but I'm trying to figure out the best way to migrate a pretty simple structure from mysql to mongodb. Let's say that I have a main table in mysql called 'articles' and I have two other tables, one called 'categories' and the other 'category_linkage'. The categories all have an ID and a name. The articles all have an ID and other data. The linkage table relates articles to categories, so that you can have unlimited categories related to each article.

From a MongoDB approach, would it make sense to just store the article data and category ID's that belong to that article in the same collection, thus having just 2 data collections (one for the articles and one for the categories)? My thinking is that to add/remove categories from an article, you would just update($pull/$push) on that particular article document, no?

like image 575
jpea Avatar asked May 03 '12 15:05

jpea


People also ask

What is the best way to migrate from SQL Server to MongoDB?

Open SQL to MongoDB MigrationClick on the SQL Migration button in the toolbar, or right-click into a server, database or collection in the Connection Tree and select the SQL Migration option. Then select SQL → MongoDB Migration. This will open a new tab where you can configure and execute the import.

Can MongoDB replace MySQL?

It's unlikely MongoDB will completely replace MySQL, but it's possible that both structured and unstructured databases will be used for different purposes in one environment. Developers interested in enterprise programming should learn both platforms to stay competitive in the job market.

Why MongoDB is preferred over MySQL?

Why is using MongoDB better than using MySQL? Organizations of all sizes are adopting MongoDB, especially as a cloud database, because it enables them to build applications faster, handle highly diverse data types, and manage applications more efficiently at scale.


2 Answers

In my opinion, a good model would look like this:

{"article_name": "name",
 "category": ["category1_name", "category2_name", ...],
 "other_data": "other data value"
}

So, to embed the category names directly to the article document. Updating article categories is easy, but removing a category altogether requires modifying all articles belonging to the category. If removing categories is frequent, then keeping them separate might be a good idea performance-wise.

This approach makes it also easy to make queries on the category name (no need to map name to id with a separate query).

Thus, the "correct" way to model the data depends on the assumed use case, as is typically the case with mongodb and other nosql databases.

like image 56
jhonkola Avatar answered Sep 20 '22 20:09

jhonkola


If you have access to a Mac computer, you could give the MongoHub GUI a try. It has an "Import from MySQL" feature.

like image 23
thomas Avatar answered Sep 21 '22 20:09

thomas