MongoDB for Developers with Python Transcripts
Chapter: Mapping classes to MongoDB with the ODM MongoEngine
Lecture: Introducing ODMs

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Now we've got to a serious place in the course where we're going to write some real code and work with some realistic complex demos,
0:08 and we're going to do that using what's called an odm, an object document mapper.
0:12 So an object document mapper is like an orm, an object relational mapper but in NoSQL, we don't have relations we have documents
0:20 so we're going to map documents to objects rather than navigate and traverse these relationships, and the one we're going to focus on for this course,
0:27 my favorite one, I think is one of the best, if not the best is something called MongoEngine.
0:34 So before we get to MongoEngine, let's just look at the overall goal here, and the features of the odm vary whether you're talking MongoEngine
0:41 or something different, but they generally follow the same principles. So with PyMongo, this is the low level api
0:48 we have our app and we have PyMongo, we'll talk to the database; so when we write a query that talks to PyMongo,
0:54 we work in the Python api and we send Python dictionaries to it, which either have the prototypical documents in it
1:01 or the high level operators, in place update operators and things like that like $addToSet,
1:07 but in order to do that, we basically write in the raw MongoDB api, as we've seen the only real change that we go through
1:15 to go from the raw shell api of Javascript over to Python is we're working with dictionaries and not json, and we're working with Pythonic names,
1:23 so insert_one has the underscore and lower case in Python, not in Javascript, but this means you're working at a super low level
1:31 and for certain operations and some of the time this makes tons of sense, it is one of the faster ways to work with MongoDB from Python.
1:39 However, most of the time, we much, much prefer having high level concepts in our application that enforce additional things
1:48 that automatically use the best features of the PyMongo and MongoDB api without us having to think about how that happens.
1:56 So that's when we can bring in an odm, we have the same thing, we got our app, now we're going to have our odm plus PyMongo,
2:03 we're going to issue a query, but this time we're not going to write in raw api code we're going to actually issue the queries in terms of classes,
2:10 think SQLAlchemy, think Django orm type of queries here. So we might have a book class given our previous example,
2:19 so we'd go to the book and we'd say find based on the isbn equals this and so on, all right, so it's very similar to the Django orm
2:28 and some of the other orms that you might be familiar with. So we work in these high level classes, and that's great
2:33 and it translates of course down to the PyMongo api, what's better though, what's really great is
2:39 it actually leverages the really advanced in place operator, so at least speaking of MongoEngine specifically now,
2:45 if we say pull back a class, an instance of a class and we make a change to say for the book, we change the title and we call save,
2:52 it's actually going to do a dollar set operation, in place update it's not just going to push the whole book back into the database
2:59 with all the optimistic concurrency issues you might run into, no, it's going to make the changes in the best way possible.
3:05 So we'll see that we'll be able to use these advanced operators without actually doing anything other
3:11 than just working with classes and objects in memory, it's really really sweet; we'll also have additional features,
3:16 it automatically works with indexes for us, it will automatically add type checking and other types of constraints
3:23 that simply don't exist in the database, but can be managed at the class level in the object level and described there.
3:29 So here's the string field, here's an integer field and the integer has to be greater than ten,
3:34 all of that stuff can be done through MongoEngine, in our application but the concept of that doesn't even exist in MongoDb, right,
3:40 so you get a lot more structured, a lot more safety in it by basically describing your schema in classes
3:47 and long as you stick to one application or share that class definition across applications, you're in a much safer place
3:54 than just randomly sending dictionaries at the database. So this odm style of programming, I find it to be extremely productive,
4:02 very clear and quite safe, neat, fast for most of what we got to do, that's really my favorite way to work with MongoDB,
4:10 and I hope you'll see the power of it and enjoy it after we go through in this chapter.

Talk Python's Mastodon Michael Kennedy's Mastodon