skarim
skarim

Reputation: 95

Migrating database structure from MySQL to MongoDB

I'm in the process of migrating my application's database from MySQL to NoSQL(MongoDB). However, I'm a bit confused as to how I should structure my MongoDB.

In my current MySQL setup, I have two tables, one for users and one for calendars. Each user and calendar have a unique ID. Each user entry contains a list of the calendar IDs associated with that user. Each calendar entry also contains the ID of the user who made it.

The problem I am facing is that I have two situations: For the login console, I must quickly show the user all the calendar he/she has created; for the frontend website, I need to display all available calendars with their author.

In MySQL I would use JOINs to query this data really quickly. How do I achieve this same functionality in MongoDB (or any NoSQL database)?

Upvotes: 1

Views: 452

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230306

You almost came to the conclusion. There are only two options without joins.

  1. Denormalize (store calendars embedded in a User document)
  2. Make separate queries (first load user, then his calendars)

Both approaches have their pros and cons, which were discussed many times (try googling for " mongodb embed or reference")

In short: embedding gives you quicker reads, referencing gives you richer queries and easier/faster writes.

Upvotes: 2

Related Questions