MongoDB has released a new shell called mongosh, which is designed to provide an enhanced user experience for working with MongoDB databases. The new shell is intended to replace the existing mongo shell and provides several new features, including better syntax highlighting, improved error messages, and enhanced auto-completion. In this article, we will see how to install a mongosh shell within our system and use it to perform MongoDB operations.
1. Install MongoDB Shell: Mongosh
Firstly, we need to figure out in the installation folder of MongoDB whether mongosh.exe is installed there or not rather than mongo.exe. If the mongosh.exe is not found then we have to install it on our own. We must go to MongoDB’s official website. There, we can see the different options on the header. We have to press the product option and it will generate the different other options for different purposes as shown in the image. Now, we can see the shells option highlighted on the screen which is underlying in the Tools options.
Once, we click on the shell option, then it will redirect us to the Download page as displayed on the image. Click on the Download Now button to download the shell.
The MongoDB shell download page is now open and here we can see the installer file of the new MongoDB shell. We can select the version and the platform according to our requirements. Simply select the Download option found at the bottom of the subsequent screen.
When the installation process has been done, then we need to extract the zip folder of the mongosh and select the destination as shown below.
Here, we can see the mongosh.exe application in our system. This application is placed on the path mongosh-1.6.1-win32-x64.zipmongosh-1.6.1-win32-x64bin where we can open it.
When we click the mongosh.exe application to open, it will launch the mongosh shell as in the following screen. All we need to press enter to establish the connection with MongoDB.
We can see the connection string with further information by following the above step. Now, we are in the mongosh shell and mongosh shell ready to perform the MongoDB operations.
Additionally, this link can be used to install MongoDB Shell.
2. Show Databases and Collections in MongoDB Shell
As we have done with the installation process in the above section. Although, we have accessed MongoDB with the mongosh shell. Now, we have executed some basic commands of MongoDB such that to get the already existing databases. We have run the following command.
#List all databases
show dbs
The above command lists all the databases here.
Similarly, we can get all the collections here by using the following command of MongoDB.
#List all collections
show collections
Hence, the output listed the collections below.
3. Switched the Database in Mongosh Shell
Moreover, we can switch to the desired database in the mongosh shell. The following command was used to switch to the provided database or to create the database if it didn’t already exist.
#Switch to database
use StudentDatabase
Here, we have switched to the studentDatabase.
4. Execute JavaScript Code in Mongosh Shell
Additionally, we can perform limited JavaScript code within the context of the MongoDB shell. To execute a simple JavaScript REPL that gets the length of the specified string, we can enter the following command in the MongoDB shell.
#Execute JavaScript Code
“MongoDB”.length
The above line uses the built-in length property of the string object MongoDB to get its length which is 7 as displayed in the following mongosh shell screen.
If you want to import JSON files using the Mongoimport command in MongoDB, you can use the following link.
5. Conclusion
In conclusion, Mongosh shell support for multiple languages, improved interface, and useful features make it a great tool for anyone working with MongoDB in their development projects. We have used the installed mongosh shell here and executed simple commands of MongoDB in it.
MongoDB has released a new shell called mongosh, which is designed to provide an enhanced user experience for working with MongoDB databases. The new shell is intended to replace the existing mongo shell and provides several new features, including better syntax highlighting, improved error messages, and enhanced auto-completion. In this article, we will see how Read More MongoDB