Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: MongoDB edition
Lecture: Importing the data from SQL to Mongo

Login or purchase this course to watch this video and the rest of the course contents.
0:00 It's looking like our app is working pretty well. If we go and run it, try to pull up the home page here you see, hey it looks like it's working.
0:08 It turns out when we have actual data there's a few minor tweaks we're going to have to change. That's not a big deal.
0:14 The bigger deal is that, well, there's no data here. We have zero projects, zero releases, zero users. How do we get that data in there?
0:22 We did have this load_data here where we took a JSON file and imported into SQLAlchemy but that's not what we're after.
0:30 What we want to do is take the data that's in SQLAlchemy and put it in to MongoDB. So I'm going to show you a cool technique
0:35 how you can take any database you connect to through SQLAlchemy, go query all the data and then transform that into MongoDB
0:43 MongoEngine classes and save that into MongoDB. Basically, migrate data from one database type to the other. So let's go create another script here.
0:58 So we have this file, now it's a little bit complicated and not really worth you watching me type it in so I'm going to paste something here
1:04 and we're going to talk through it real quick. So we're going to go and initialize both database connections. One to MongoDB, as well as SQLAlchemy.
1:13 Because we need to read the data from SQLAlchemy or SQLite and then push it into, save it into MongoDB.
1:19 And then, these three functions here, they all work basically the same. The first question is, are there any users?
1:26 If there are, we probably already run this don't have to do that. So be careful of the start with zero but then it will not override it.
1:33 Then we just go and do a query against SQLAlchemy for all the users. Get them all and then we loop over them, one at a time
1:40 we create a MongoDB user, set the properties and then call save. Hash password is SQLAlchemy's hash password. Name is SQLAlchemy's name. And that's it!
1:50 If you have a ton of data insert, doing a save one at a time can be pretty slow, so you can do bulk inserts as well with MongoEngine and what not
1:59 we don't have that much data. We're not doing it. All the others, they're pretty much the same packages, and releases, and so on.
2:06 So I want to show you a quick trick we're doing up here that's kind of cool. We're working with package, right?
2:11 So here's a package class and here's a package class how in our code do we know which is which? Well this one is the MonogDB one and this one
2:18 is the SQLAlchemy one. So we're importing is as SQL package Mongo package or SQL user and Mongo users.
2:23 That's just a cool technique to help you remain sane while you're trying to run this. So let's go ahead and run this and see what happens.
2:33 Takes a second, it's doing some work, and it's done! So let's go look over here and we can do our query again.
2:43 Go show me all the packages. Ta-dah! There they are. How cool is that? We're got our ID name and email created.
2:49 We didn't copy over maintainers, because it turns out we we're storing them in SQLAlchemy, so there's really no way to copy them over.
2:55 Also, MongoEngine does not insert unmodified values. Okay, so notice that some of the values that are not here they're simply just not in there.
3:06 This is the package we want to move on but still, we don't have the maintainers copied over. Right? You can see it's just the values that we've set.
3:13 These times are the ones that come out of SQLAlchemy not just the default ones, right? So we copied all this over, saved in and it's good to go.

Talk Python's Mastodon Michael Kennedy's Mastodon