MongoDB with Async Python Transcripts
Chapter: Beanie Quickstart
Lecture: Beanie Quickstart: Part 3 Inserts

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's talk a little bit about inserting new data. So again, we're going to copy this and edit on, so you have it saved at different points.
0:11 Make sure we're running part three. And we are. So right now we have create a user. Let's do insert multiple users.
0:27 This one doesn't exist yet, but it's going to be really similar to create a user.
0:31 So we'll jump down there and we'll just duplicate that, Command or Control D in PyCharm. I'm going to call this one insert multiple users.
0:43 So here if we have four or more users greater than or equal to four, you already have however
0:51 many users you have, because we want to, we're going to create a set of users, different
0:58 users. Now we had this one before, but let's actually get three different ones and insert
1:03 them into the database. So here you can see I've copied over three different accounts,
1:16 just one, we got user one, two, and three, it's Michael as before. We have Sarah from
1:22 Tennessee, and we have Kylie from Baden-Württemberg in Germany. We're going to insert all three
1:28 of those into the database here. Now one option, we could do just what we had before, u1.save,
1:37 and u2.save, and u3.save. That'd be fine. That makes three separate calls to the database.
1:45 might want to do this all in one shot. So in addition to just saving or inserting them
1:50 one at a time, remember, save will insert it if it's new, and it will update it if it
1:56 just has changes but already has a primary key and it's come out of the database. Another
2:02 option is we can go to the object, the type itself that models that collection in MongoDB,
2:08 in this case, the user. And you can see that there's options on here as well. So we can
2:14 do things like insert many, anything that has a CLS as the first parameter, these are things you can do on top of just the type itself, right?
2:25 So you have, that's how you do your queries, you can see find and find one. But what we want is insert many.
2:31 And here we can just put you one, you two, you three, and I guess it's probably most efficient as a tuple instead of a list.
2:40 But it doesn't really matter. Now, PyCharm is saying, you know, this thing here, you really should be awaiting it. Coroutine is not awaited.
2:49 Remember, anytime you interact with the database, you need that. Expected a list. All right, I'll make it a list just to make it happy.
2:59 I'm not convinced it really needs it. But let's make it a list. So here we go, we're going to insert many into the database.
3:09 So when we checked whether we could insert the one that was already there, and now we
3:13 created a set of users and let's go back to the database and have a look here. Run it again. And now if we go to tree view, we've got multiple ones.
3:27 And guess what? This is probably Kylie from Baden-Württemberg location, expanded that out. Yeah, sure enough. And here's Sarah, right?
3:36 We inserted all of them. Beautiful. We have two of these options for inserting them one at a time through save or we can insert many.
3:47 Just a side note, I changed this to have optional city because my little preloaded data didn't have it.
3:54 All right, so that's how you insert data either one at a time as we have on the screen right here or bulk writes like this.


Talk Python's Mastodon Michael Kennedy's Mastodon