简体   繁体   中英

Best practices for transforming a relational database to a non relational?

I have a MySQL Database and I need to create a Mongo Database (I don't care about keeping any data).

So are there any good practices for designing the structure (mongoose.Schema) based on the relational tables of MySQL?

For example, the SQL has a table users and a table courses with relation 1:n , should I also create two collections in MongoDB or would it be better to create a new field courses: [] inside user document and create only the user collection ?

The schema definition should be driven by the use cases of the application.

Under which conditions is data accessed and modified. Which is the leading entity.

eg When a user is loaded do you always also want to know the courses of the user? This would be an argument for embedding.

Can you update courses without knowing all of its users, eg update the name of a course? Do you want to list an overview of all courses? This would be an argument for extracting into an own collection.

So there is no general guideline for such migration as only from the schema definition, the use cases cannot be derived.

If you don't care about data, the best approach is to redesign it from scratch.

NoSQLs differ from RDBMS in many ways so direct mapping will hardly be efficient and in many cases not possible at all.

First thing you need to answer to yourself (and probably to mention in the question) is why you need to change database in the first place. There are different kind of problems that Mongo can solve better than SQL and they require different data models. None of them come for free so you will need to understand the tradeoffs.

You can start from the very simple rule: in SQL you model your data after your business objects and describe relations between them, in Mongo you model data after queries that you need to respond to. As soon as you grasp the idea it will let you ask answerable questions.

It may worth reading https://www.mongodb.com/blog/post/building-with-patterns-a-summary as a starting point.

An old yet still quite useful https://www.mongodb.com/blog/post/6-rules-of-thumb-for-mongodb-schema-design-part-1 Just keep in mind it was written long time ago when mongo did not have many of v4+ features. Nevertheless it describes philosophy of mongo data modelling with simple examples.It didn't change much since then.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM