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


Talk Python's Mastodon Michael Kennedy's Mastodon