MongoDB for Developers with Python Transcripts
Chapter: MongoDB's shell and native query syntax
Lecture: Connecting to MongoDB with the shell

Login or purchase this course to watch this video and the rest of the course contents.
0:01 So let's connect to MongoDB, I already have it running as a separate process hidden away, we'll talk about how to run MongoDB later,
0:09 you should have seen in the setup how to get it started and then we'll talk about the deployment side of things later in the class.
0:15 So MongoDB is running, it's running the local machine under default ports, no security, nothing like that for getting started,
0:22 it's only listening on so it's not listening on the public network, on my machine, so for that reason, more or less plus firewalls,
0:32 the authentication part we're going to turn off for a little bit, just so we can start from the beginning;
0:38 okay, the other thing I have is I have set up MongoDB in my path, so I can ask which Mongo, and it comes back with something,
0:46 so what I actually did is I went to MongoDB and I just downloaded the tarball, and I unzipped it,
0:52 and I sort of changed the naming around, so it's in this path here, so here's the actual executable.
0:56 Mongo is the name of the shell, mongod is the name of the server for deamon
1:00 so in order to connect to MongoDB, there's a ton of options we could give it
1:04 and like I said, when we get to the deployment and production stuff at the end, we'll have to pass all sorts of things like authentication,
1:10 an ssl flags, and whatnot, server names here but in the beginning, we can just type mongo. And you'll see, right here, we're running 3.4.4
1:20 and it's connected to local host 27017, that's the default port for standalone servers, there's 27 thousand, 18, 19 and 20 are reserved
1:31 or typically the default for other types of things. So my system is not exactly set up right,
1:36 but it's not a production machine it's just my dev machine, okay. So now we're connected, what do you do?
1:41 Well, probably the first thing you want to do is focus on a particular database, so you can say show dbs
1:46 and it will show you the various databases, how large they are things like that,
1:51 so we're going to work with the bookstore for our examples in this chapter. Later, we're going to work on something that maps over to a car dealership,
2:00 so those are the two databases that we're going to be working with, you can see that I have got some for my various sites here and things like this,
2:07 I have actually broken it apart so like Talk Python the core data it's not really zero gigs, it's just rounding down, it's like 20 MB or something,
2:15 but the analytics is half a gig here, and it's actually much more if you export it.
2:21 So we may have more than one database for our app like I have on my podcast, or you might just have one for the trading site, like we do here.
2:28 Great, so now I want to maybe find a book in the bookstore, so how do I do that— the first thing you have to do is
2:35 you have to activate the database, so you're going to say db.command, whatever that is, and give it some command here,
2:41 where db refers to one of these databases, so the way we do that is we say use say bookstore, like this, now it says great, we switched to bookstore,
2:50 and then we could say db. first of all what are the equivalent of tables in MongoDB these are called collections, because they're not tabular,
2:57 so we can say show collections, and this is what is contained inside of bookstore, there's a Book, case sensitive, Publisher, Test and User, ok.
3:07 So if I wanted to find the books let's say db.Book.find let's say just limit one, so it doesn't go crazy on our shell here,
3:14 so basically, the way it works is we connect, we figure out what the database we want to work with is,
3:20 we say use that database and then we say db.collection name and then we typically fire these commands at the collection.
3:26 Now, what's interesting that is missing here is there's not like a create database or inside of
3:33 here there's not a create table or create collection command, so like Python in some ways, MongoDB is very, very dynamic,
3:42 so if we wanted to create a table, let's go and just create a collection and we won't create a whole new database,
3:48 so what database we have, we have a bookstore and we have those for collections bookl publisher, test and user,
3:53 so if I want to create one called logins— let's say just log for history I could even issue a find command against that
4:04 and there's just nothing, it's just empty. If we go up here and we say what's here, there's no log,
4:10 but if I actually try to interact with this, we'll talk about inserts in a little bit, but let's just really quickly see how this works,
4:18 I would just say let's say name or action is view, something like that, if I insert this, no just crazily this works and something was inserted,
4:27 if we look there's now a log, so db.Log, case sensitive .find, there
4:33 and it inserted this thing, action with a view and I gave it the id whatever it is, this is called an object id, we'll talk about that later.
4:40 Okay, so this shell is how we work with MongoDB, if I want to get rid of it, I could go here and say drop collection, just drop, right,
4:55 and now log is gone again. So this is your base level admin tool and it works everywhere, so we could ssh into our Linux server
5:03 Digital Ocean, or on aws or whatever, and we could do this, we could even sort of tunnel this through there,
5:11 but we're going to see that there is actually some better options any time we're running somewhere where we can even just tunnel over to the server.

Talk Python's Mastodon Michael Kennedy's Mastodon