Mongo shell is a tool bundled with the MongoDB server (mongod) to perform simple CRUD on your database from the command line. You will find this tool with name "mongo" in the same folder as the server binary "mongod". Although most people might opt to go with a GUI based tool, we still believe mongo shell is a very robust and straight forward tool to use. At NodeChef, we only use mongo shell internally without any GUI tools. Mongo shell gives us all we need to work with MongoDB.
In this article, we will assume you currently have a MongoDB database running and you have the connection string. Your database does not have to use NodeChef MongoDB hosting, this article should apply whichever cloud your database is housed in.
Connection string format.
Your database connection string will typically look like this:
The above connection string is what you will get if you are running a replica set. If you are running a single instance, your connection string will look like this:
When we have multiple hosts, that is a replica set, we will have to connect to each host in the connection string to figure out which one is the primary and then use it. The primary reflects the current state of your data and you should always use the primary when working with your data. For single instances, we simply connect to the host and that should be our primary.
Using Mongo Shell
Depending on the operating system, and also how you installed MongoDB, the folder in which the binaries was installed will already be added to the OS PATH variable. The PATH variable simply is a list of folders where the command line will search for programs you attempt to launch which is not in the current working directory.
Determine if mongo shell is in your path by typing "mongo help" from your terminal or CMD. If you get a command not found error, this means you will have to add the folder containing the MongoDB binaries to your PATH variable yourself. For Windows operating systems, MongoDB is typically installed under "Program Files" -> "MongoDB". Find the bin folder under the MongoDB directory and that should contain the "mongo.exe" file and just "mongo" for Linux and Mac users. This article should provide you with all the details on adding the bin folder to your PATH folder for windows users and this article for Mac and Linux users. After you have added the folder, you must close and launch command prompt or terminal again.
Initiating the connection to the server
On the command line, you can use the command as shown below to connect to your MongoDB server. If you have a replica set, you can simply change the part which says "host1.nodechef.com:27017" to "host1.nodechef.com:27017" to connect to the other server in the replica set.
Once connected and we are on the primary, we should see something similar to the image below on our terminal.
The value "PRIMARY" indicates we are connected to the primary member in the replica set and we can go ahead and use it. If it said "SECONDARY" instead, you will want to exist and connect to the other host to find the replica set. If your server is not a replica set at all, that is a standalone instance, you will see neither "PRIMARY" nor "SECONDARY". This is completely fine as the instance is not part of any replica set.
Once connected, you can run commands to view edit and delete your data. For a quick reference on available commands, you can review this document. You can use the command "show collections" to view all your collections in the current database. You are now all set to use mongo shell.
You can always run "mongo help" on your terminal to view all the parameters available when connecting. This can be useful if your database server requires clients connect using TLS.