jpea
jpea

Reputation: 3079

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?

Upvotes: 5

Views: 3560

Answers (2)

jhonkola
jhonkola

Reputation: 3435

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.

Upvotes: 4

thomas
thomas

Reputation: 2578

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

Upvotes: 4

Related Questions