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 is super super straightforward, it's really delightful, so here we're going to create a car, but remember
0:08 the car requires an engine, the engine is required and the engine must be an instance of an engine object. So we're first going to create an engine,
0:16 set things like the horsepower, the liters, is the miles per gallon, so notice this is a chevy bolt which is an electric car
0:23 so we just ramp the mile for gallon like super high, liters is zero because how many does an electric engine have- none,
0:29 and I'll say it's a 120 horsepower, I really have no idea. Then we're going to create the car, its model is a bolt, its make is a chevy
0:36 and the year is 2017, and then we just pass the engine along, right engine = engine, one is a keyword value and one is just the name of the variable.
0:44 So then we have our car, and right now the id of the car is not stored in the database,
0:48 so we hit save and boom, now we have like a car with its id and its default values set all of those things stored in the database.
0:56 So this is great for inserting one car but if you are going to insert a thousand or a hundred thousand or a million cars
1:04 let me tell you, this is the slowness right here, you do not want to do this; there's a much better way, maybe you don't take a million inserts at once
1:11 maybe you bulk it up and do like 50 at a time or a 100 at a time, but if you are going to do some kind of bulk insert how do you do that?
1:17 Also super easy, let's suppose we have a list of cars that we want to insert
1:21 and I'm not showing how you initialize the cars, but same as above basically, but skip the save step, so we're going to get car one, car two,
1:28 we want to insert a bunch of them we just go car.objects.insert and give it that list and boom it does a bulk insert in MongoDB,
1:36 which if you're inserting many items is much much faster.