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