How to Use MySQL and MongoDb together

- 1 answer


I am planning of using MongoDB for my comments function. But all the user's data is in MySQL. That means, I am trying to store comments in MongoDB and other fix information in MySQL. But when I started to think about retrieving comments from MongoDB, I came across the question that, how can I relate MongoDB data with MySQL. For example, user name, profile_url is stored in MySQL comments are stored in MongoDB with user_id

So how can I retrieve data like

| name  | profile_url  | comments              |
| xyz   | image.jpg    | That was nice comment |
| abc   | image.jpg    | I agree               |

Is it possible to do so? Or is there any other way?

and I am using Laravel 5 with jenssegers/laravel-mongodb package.




MongoDB and MySQL are completely separate applications. They have no way to communicate with each other except through your application. That means if a request needs data from both sources, it needs to query both separately.

But what you could do is keep redundant data in both databases. When you store a comment in MongoDB, also put the relevant user information into the Comment document. Such a duplication of information is a deadly sin in relational databases but is common practice in MongoDB. Until recently (3.2) MongoDB had no support for JOINs whatsoever, and even now it's still quite rudimentary. That means you should usually avoid storing the data you need to fulfill a request in more than one collection, even if that means that you have redundancies.