Connecting to MongoDB - Part 4
How to connect to MongoDB using the Shell or Command Prompt.So, there is really two possibilities here. You’re working from a Linux-type shell. And by the way even Macintosh uses a bash shell or you’re using a Windows Command Prompt. The only differences are this. If you’re using the Windows Command Prompt, make sure you are running the Command Prompt as administrator. If you’re using a Linux Shell, make sure you proceed your initial commands, your actual Linux commands with the keyword sudo, so that you’re running with privileges. Now, once you’ve started and you’ve connected the Mongo and you’ve launched the Mongo Shell, the rest really doesn’t matter whether you are in Linux or Windows Visual C. They are the exact same commands. It’s actually pretty simple to connect and I am going to show you how. All we need to do is to start the shell. Now, I will give you this brief caveat. In this video, we’re assuming that you’ve previously installed, configured and started Mongo successfully. If it’s not running, you’ll obviously get an error at this point. The first thing is to make sure you’ve navigated to the bin subdirectory wherever you’ve installed MongoDB.
In a command prompt window, the prompt is currently C:\MongoDB\bin>.
The presenter enters the command mongo. The output is
MongoDB shell version: 3.2.0
connecting to: test
Then just type in the word mongo and it will open up the Mongo Shell. And it tells you the current shell version and it’s connecting to a particular database. In our case, this is the test database and that’s the one we’re going to be using throughout these videos. Now, from this point on, you’re using Mongo commands, not Windows commands or Linux commands. So, which operating system you’re using is relevant from this point on. Now, once you’ve connected, you might want to verify a few things. For example, I want to see what databases are available. Well, there is an admin in a local one. Now, if I wish to use a particular one, I would use the word use, and let’s say local. And now, I am pointed at the local database. Anything I do from this point forward is going to be in regards to the local database. It’s as simple as that to connect to Mongo.
At the prompt, the presenter enters the command show dbs.
The output is
admin(empty) local 0.078GB
The presenter then enters the command use local.
The output is
switched to db local