We know that MongoDB has two ways of modeling relationships between
relations/entities, namely, embedding and referencing (see difference here). Let's say we have a USER
database with two tables in mySQL named user
and address
. An embedded MongoDB document might look like this:
{
"_id": 1,
"name": "Ashley Peacock",
"addresses": [
{
"address_line_1": "10 Downing Street",
"address_line_2": "Westminster",
"city": "London",
"postal_code": "SW1A 2AA"
},
{
"address_line_1": "221B Baker Street",
"address_line_2": "Marylebone",
"city": "London",
"postal_code": "NW1 6XE"
}
]
}
Whereas in a referenced relation, 2 SQL tables will make 2 collections in MongoDB which can be migrated by this apporoach using pymongo
.
How can we directly migrate MySQL data as an embedded document using python?
Insights about about Pseudo code and performance of algorithm will be highly useful. Something that comes to my mind is creating views
by performing joins
in MySQL. But in that case we will not be having the structure of children document inside a parent document.