- [Instructor] Let's get started working with Mongo directly now. First, we're going to explore the Mongo Shell. Type Mongo D to start up the Mongo server if it's not already running. You'll see some startup messages and at the end it should tell you it's listening on port 27017, the default MongoDB port. Ignore any warnings you see. Since we're not running a production database, it's not necessary to secure and optimize it. Now I'm going to switch to a different terminal tab. You need to use a second terminal or command prompt for the client.
Start the Mongo shell in your terminal by typing Mongo. You'll see a few startup messages and then a command prompt, which is our cue to start making requests. Again, if you see warnings here, it's fine as long as the prompt comes up as shown here. By default, you're connected to the test database. You can see this by typing DB at the prompt. Let's use a different database for our course by typing use Learning Mongo. Now we're using the database Learning Mongo. Note that even though the Learning Mongo database didn't exist, Mongo switched our context appropriately.
When you show DBs, you can see that Learning Mongo doesn't show up yet. It's going to be completely created when the first document is inserted, so let's go ahead and do that. Mongo stores data in databases which are separated into collections of documents. To create a new collection, we simply need to insert a document and the collection will be created as a side effect. So DB, we'll put it in a cars collection, and insert, and we're going to put in Subaru.
Okay. It inserted one item. This is going to happen in the database we selected. Now we've inserted a new car into our cars collection in our database, so what effect did that have? Let's look at the show DBs command again, so you can up arrow to see previous commands. Look, Learning Mongo has shown up now. Let's see what collections we've got. So show collections is how you do that, and we have cars. So we have the Learning Mongo database and we have the cars collection.
Let's see if we can set a variable to use later. So we're going to set an array to one, two, three. And let's take a look at that variable now. So you can set variables to use them later on in the execution. While you're unlikely to want to do anything particularly intense using this shell, it does make it possible for you to programmatically interact with the database. Let's create a very basic collection of numbers.
It's going to take some time to complete, so just be patient. We're inserting 10,000 records into the database. So we'll start with a loop, four I equals zero, I smaller than 10,000, I plus plus, open curly brace, db dot numbers dot insert, and we're going to put in number I close the parentheses and close the curly brace.
So DB number count. 10,000, great. Let's find a single document in the database. We'll go ahead and grab the one with the number one. DB numbers find number one. Let's take a look at what Mongo's doing with the query. Hit the up arrow to see the query again and we'll add some debugging. So we're going to do a find with the number one and we're going to say explain.
So we got a lot of information here about the query planner, it tells us how it was planning to figure out what query to run, but it didn't tell us much about the actual execution. So what we're going to do is we're going to say explain execution stats. And we get a little bit more information and you can see it examined all 10,000 documents and it only took seven milliseconds because there were not that many documents or keys to look at, but we don't want it to examine 10,000 documents in order to return a single one.
Let's add a simple index to see how it changes the outcome. So DB dot numbers dot create index number colon one. Now notice this is very similar to what we had before, but we're creating an index, not doing a find, and the number is a special variable. We're not using number as a string. So we created an index, it didn't have to create the collection automatically cause we've already created the numbers collection, and let's try the execution stats again and see what happened.
Now I'll scroll up a little bit so that we can see it. Since we were putting an index on exactly what we were searching for, it only had to examine one document in order to find what it was looking for. In a 10,000 database it doesn't make a huge difference in how long it takes to run, but in a larger database, it can make a huge difference. Using this simple index reduces the execution time to almost nothing even for very large datasets. Now we're going to start working with actual data in the database.
- Installing MongoDB for Windows and OS X
- Why Mongo?
- Document-oriented data
- Exploring the Mongo shell
- Importing data into the database
- Building an application in Node.js
- Tuning Mongo queries
- Replication and sharding