MongoDB for Developers with Python Transcripts
Chapter: Mapping classes to MongoDB with the ODM MongoEngine
Lecture: Concept: Inserting objects with mongoengine
0:00 We saw that inserting in MognoEngine
0:02 is super super straightforward, it's really delightful,
0:05 so here we're going to create a car, but remember
0:07 the car requires an engine, the engine is required
0:10 and the engine must be an instance of an engine object.
0:13 So we're first going to create an engine,
0:15 set things like the horsepower, the liters, is the miles per gallon,
0:19 so notice this is a chevy bolt which is an electric car
0:22 so we just ramp the mile for gallon like super high,
0:25 liters is zero because how many does an electric engine have- none,
0:28 and I'll say it's a 120 horsepower, I really have no idea.
0:32 Then we're going to create the car, its model is a bolt, its make is a chevy
0:35 and the year is 2017, and then we just pass the engine along,
0:38 right engine = engine, one is a keyword value and one is just the name of the variable.
0:43 So then we have our car, and right now the id of the car is not stored in the database,
0:47 so we hit save and boom, now we have like a car with its id and its default values set
0:52 all of those things stored in the database.
0:55 So this is great for inserting one car
0:58 but if you are going to insert a thousand or a hundred thousand or a million cars
1:03 let me tell you, this is the slowness right here, you do not want to do this;
1:07 there's a much better way, maybe you don't take a million inserts at once
1:10 maybe you bulk it up and do like 50 at a time or a 100 at a time,
1:13 but if you are going to do some kind of bulk insert how do you do that?
1:16 Also super easy, let's suppose we have a list of cars that we want to insert
1:20 and I'm not showing how you initialize the cars, but same as above basically,
1:24 but skip the save step, so we're going to get car one, car two,
1:27 we want to insert a bunch of them we just go car.objects.insert and give it that list
1:32 and boom it does a bulk insert in MongoDB,
1:35 which if you're inserting many items is much much faster.