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