MongoDB for Developers with Python Transcripts
Chapter: MongoDB's shell and native query syntax
Lecture: MongoDB's query syntax
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
So we've talked a lot about NoSQL document databases and MongoDB.
0:05
Now it's time to actually start using MongoDB.
0:08
So what we're going to learn in this chapter is twofold:
0:11
one, how do you connect to it and manage it,
0:13
with the management tools if you will,
0:16
that is more or less the shell, and some additional tools,
0:19
but also how do you query it from that shell.
0:22
So maybe in Python in a traditional relational database
0:25
you might be using say SQLAlchemy to talk to a relational databases,
0:29
so you wouldn't necessarily use SQL, the language, in Python
0:32
but if you want to connect to the database directly and work with it
0:35
then you need to use ddl and SQL and things like that,
0:38
there is the same parallel here in that we're going to use the shell
0:41
and we need to use MongoDB's native query syntax
0:44
which turns out to be very similar to Python's under certain circumstances,
0:47
so it's going to be serving dual purpose there.
0:50
So the primary MongoDB shell is a command line tool, right,
0:55
we just type mongo name of the server, some connection string options,
0:58
you can see all that the title here in this terminal.
1:02
And then we just issue commands like if I want to go and use
1:05
the training database out of the server, I'd say use training;
1:09
and if I want to say go the courses and find
1:12
the one with id 5 and display it not in a minimized, minified,
1:16
but in a readable version, I would say db.courses.find
1:20
and I'd give it the little json thing, id is 5 and I'd say pretty,
1:25
So this is going to be entirely done in Javascript,
1:28
so these statements that you type here,
1:31
although you don't see any semicolons,
1:33
these are either shell statements like use training
1:36
otherwise, they're entirely pure Javascript.
1:39
So what we're going to do is we're going to learn the Javascript api
1:43
to talk to MongoDB, to query MongoDB,
1:45
to do all the crud operations, there's a find, there's a delete,
1:49
there's an insert, there's an update, of course there's sorts, there's upserts,
1:52
there's all the things you would do in a standard database,
1:55
the query syntax uses sort of a json model to help represent
1:59
either operators or hierarchies and things like that.
2:03
Now, you may be thinking, Michael, I came to a Python course,
2:06
I don't want to learn the Javascript api, I want to learn the Python api—
2:09
you will, you will learn the Python api for sure,
2:12
and luckily, it's really, really similar, it's not identical,
2:15
they made the Pythonic api Pythonic
2:18
and the Javascript one follow the idioms of Javascript,
2:20
but nonetheless, other than the slight like variations
2:23
in naming around those ideas, they're basically identical,
2:26
in Python we would use {_id : 5 } as a dictionary,
2:31
here we use it as a json object;
2:34
so on one hand, learning the Javascript api
2:36
it is more less learning the Python api.
2:38
But on the other, if you work with MongoDB,
2:41
if this drives your application and you actually work with Mongo, in a real way,
2:45
you will have to go into the shell, you will have to talk to the database directly,
2:49
you have to maintain it, and manage it, and back it up, and do all those things;
2:52
in order to do that, you need to know the Javascript capabilities,
2:56
the way to do this in Javascript, as much as you do the Python way.
3:00
Ultimately, the end game is to use something like MongoEngine
3:03
which is equivalent to SQLAlchemy, sort of analogous to SQLAlchemy,
3:08
in that we won't even be speaking in this syntax,
3:11
but still, you'll need to know how these translate down into these queries
3:15
because you might want to say add an index
3:17
to make your MongoEngine perform much, much faster, things like this.
3:21
So we're going to focus on Javascript now, and then for the rest of the class,
3:26
we're going to basically be doing Python, but like I said,
3:29
in order to actually use, manage, run,
3:32
work with an application that lives on MongoDB,
3:34
you have to be able to use the shell, and to use the shell you do Javascript.
3:38
So just like anybody who writes web apps, we're all Javascript developers,
3:41
if we write any form of web app, similarly here,
3:44
if you work with MongoDB, we're all Javascript developers
3:47
and we got to do just a tiny bit, but you'll find it like I said,
3:49
it's super, super similar to what we're going to do in Python.