MongoDB for Developers with Python Transcripts
Chapter: Course conclusion
Lecture: Lightning review: mongoengine

Login or purchase this course to watch this video and the rest of the course contents.
0:00 After we talked about document design
0:03 and we talked about the raw access from PyMongo
0:05 we said let's take this up a level of abstraction,
0:08 let's actually build classes and map those over ORM style into MongoDB.
0:14 We saw a really nice way to do that is with the ODM called MongoEngine.
0:19 Let's review the main way that we sort of define classes
0:23 and add constraints and things like that.
0:25 Over here we are going to create this car object, this is our dealership example
0:30 and we are going to store the car in the database.
0:33 The way we create something that MongoEngine can manage
0:37 in MongoDB as a top level document,
0:40 is that we're going to derive from mongoengine.document.
0:43 And then every field is going to be one of these fundamental field types,
0:46 like StringField, IntField, FloatField and so on.
0:50 And we can have some of them required, the first three required,
0:53 we can have some of them with basic default values, like mileage defaults to zero
0:59 but we can also have interesting functions,
1:01 for example the vin number is automatically generated
1:04 and we're based in this on the uuid4 random alphanumeric thing,
1:08 so what we have here so far is really sort of equivalent
1:11 to what you might have in a traditional relational database,
1:15 there's entry and there is a flat set of what you would call columns,
1:19 this is only part of the story,
1:21 remember we can have nested documents,
1:24 we can have actually a rich hierarchy of nested objects.
1:27 One thing we might want to store in the car is an engine
1:30 and the engine itself is a special type,
1:33 here in the field it's going to be an embedded document field
1:36 an engine derives from mongoengine.EmbeddedDocument,
1:40 not document, embedded document.
1:42 These we're never going to directly insert into the database,
1:44 in fact, we're going to always put them into a car,
1:48 so this is like a strong relationship between a car and its engine,
1:51 we can even mark it as required.
1:53 Now going a little further than that,
1:55 our service history actually contains a list of subdocuments,
1:58 each one modeled by the service record.
2:00 The service record has things like the customer satisfaction,
2:03 what service was performed and so on.
2:06 Now if we take this, put some appropriate data into it and store it,
2:10 we'll get something looking along the lines of this,
2:12 in our document database in MongoDB,
2:15 so here we have the first few elements that are just the flat fields
2:18 and then we have the nested engine, one of them,
2:21 we have the nested array of nested items for the service histories,
2:24 and this really gets at the power of MongoDB,
2:28 this nesting and these strong relationships
2:31 where you get this aggregate object the car,
2:34 that always contains everything we need to know about it.
2:37 How about queering— we're not going to write now in the low level api,
2:42 we're going to use basically the properties of these objects.
2:46 Here's the function that we wrote where we wanted to ask the question
2:49 what percentage of cars have bad customer rating,
2:53 that would be average or below,
2:56 so we're going to go to the car and we say objects,
2:58 we could do lots of these objects.filter.filter.filter
3:02 but if you just have one query you can just stick it in object,
3:04 so as the objects service_history, now we can't say dot here,
3:08 because service_history . customer_rating
3:10 would not be a valid variable name or parameter name in Python,
3:13 so we're going to traverse a hierarchy with a double underscore.
3:17 We also might want to apply one of the operators,
3:18 in this case we're going to say less than 4,
3:21 so we're going to use again this double underscore,
3:24 but in this case it's going to say on the left is the name of the target
3:28 and on the right is the operator we're going to apply to it.
3:31 You don't put the dollar again, that wouldn't be valid in Python,
3:34 but double underscore __lt, and then we can ask
3:38 things like count, or go and get the first one, or things like that.
3:42 We can even do paging by slicing on that result.
3:45 This syntax lets us use almost the entire spectrum of the way of creating MongoDB
3:50 really straightforward and in a way that ties back to the car object that we defined.