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