{"id":105823,"date":"2025-04-10T00:48:45","date_gmt":"2025-04-10T00:48:45","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=105823"},"modified":"2025-02-25T00:53:44","modified_gmt":"2025-02-25T00:53:44","slug":"working-with-mongodb-shell","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/databases\/nosql\/mongodb\/working-with-mongodb-shell\/","title":{"rendered":"Working with MongoDB Shell"},"content":{"rendered":"<p><strong>This article is part of Robert Sheldon's continuing series on Mongo DB. To see all of the items in the series, <a href=\"https:\/\/www.red-gate.com\/simple-talk\/collections\/robert-sheldon-ongoing-mongodb-primer\/\">click here<\/a>.<\/strong><\/p>\n\n\n\n\n<p>Throughout this series, I\u2019ve provided a number of examples of how to run commands in MongoDB Shell (mongosh). However, the examples were limited to the version of mongosh that\u2019s embedded in MongoDB Compass. Although mongosh can be a handy tool to use in conjunction with Compass, MongoDB also offers a freestanding version of mongosh that provides greater flexibility and gives you more control over the command environment.<\/p>\n\n\n\n<p>To use the freestanding version of mongosh, you must first install it on your local computer. MongoDB provides versions for Windows, macOS, and several Linux distributions, any of which you can download for free. After you install mongosh, you\u2019ll be able to access it through your system\u2019s command-line interface (CLI), where you can connect to a local MongoDB instance or to a MongoDB Atlas cluster.<\/p>\n\n\n\n<p>In this article, I demonstrate how to get started with mongosh and use it to run commands and script files. I also explain how to configure the shell\u2019s settings and command prompt. To follow along, you must be able to download and install mongosh on your system, as well as access your system\u2019s CLI. Beyond that, you should find it fairly straightforward to work with mongosh.<\/p>\n\n\n\n<p>Note: For the examples in this article, I used the same MongoDB Atlas environment I used for the previous articles in this series. Refer to the first article for details about setting up this environment.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-installing-mongodb-shell-mongosh\">Installing MongoDB Shell (mongosh)<\/h2>\n\n\n\n<p>You can run mongosh on Windows, macOS, and Linux systems. Linux support includes Ubuntu, Debian, Amazon Linux, Red Hat Enterprise Linux (RHEL), and other distributions.<\/p>\n\n\n\n<p>Not surprisingly, the process of installing mongosh differs from one OS platform to the next. In addition, each platform supports multiple installation methods. In Windows, for example, you can install mongosh from an .msi or .zip file. In macOS, you can use Homebrew or install it from a .zip file. And in Linux, you can install mongosh from a .deb, .rpm, or .tgz file.<\/p>\n\n\n\n<p>The steps you take to install mongosh on your system will depend on both the operating system and installation type. For instance, you can quickly install mongosh on a Windows 11 computer in just a few steps:<\/p>\n\n\n<div class=\"block-core-list\">\n<ul class=\"wp-block-list\">\n<li>Download the .msi file.<\/li>\n\n\n\n<li>Double-click the downloaded file.<\/li>\n\n\n\n<li>Follow the prompts to complete the installation.<\/li>\n<\/ul>\n<\/div>\n\n\n<p>You can find information about the different installation options in the MongoDB topic <a href=\"https:\/\/www.mongodb.com\/docs\/mongodb-shell\/install\/\">Install mongosh<\/a>, which provides specific details about the supported operating systems and installation packages. In addition, you can download installation files at <a href=\"https:\/\/www.mongodb.com\/try\/download\/shell\">MongoDB Shell Download<\/a>, where packages are available for the different operating systems. If you\u2019re using a package manager such as Homebrew, you won\u2019t need to download a file in advance.<\/p>\n\n\n\n<p>Whatever option you choose, be sure to follow MongoDB\u2019s online instructions for installing mongosh in your environment. Also follow any prompts that are provided as part of the installation process. Again, the exact steps you take will be specific to your system and package type.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-connecting-to-mongodb-atlas\">Connecting to MongoDB Atlas<\/h2>\n\n\n\n<p>Once you\u2019ve installed mongosh, you should be able access it immediately through a CLI such as Command Prompt in Windows or Terminal in Ubuntu or macOS. Whenever you launch mongosh, however, you must establish a connection to a MongoDB deployment, either a local instance or an Atlas cluster.<\/p>\n\n\n\n<p>For the examples in this article, we\u2019ll be using Atlas, as we\u2019ve been doing throughout this series. To connect to Atlas, you should first retrieve the connection string from the Atlas interface, like you did when you initially connected to Atlas in MongoDB Compass.<\/p>\n\n\n\n<p>To retrieve the connection string, log into the Atlas website and select the applicable organization and project. For the examples in this series, I\u2019ve been using the <strong>org1<\/strong> organization and the <strong>proj1<\/strong> project.<\/p>\n\n\n\n<p>On the project\u2019s <strong>Overview<\/strong> page, click the <strong>Connect<\/strong> button, which is outlined in red in the following figure.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1904\" height=\"1152\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-1.png\" alt=\"\" class=\"wp-image-105824\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-1.png 1904w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-1-300x182.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-1-1024x620.png 1024w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-1-768x465.png 768w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-1-1536x929.png 1536w\" sizes=\"auto, (max-width: 1904px) 100vw, 1904px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Clicking the button launches the <strong>Connect<\/strong> dialog box, where you\u2019re presented with multiple connection options, as shown in the following figure.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1622\" height=\"1652\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-2.png\" alt=\"\" class=\"wp-image-105825\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-2.png 1622w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-2-295x300.png 295w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-2-1005x1024.png 1005w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-2-768x782.png 768w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-2-1508x1536.png 1508w\" sizes=\"auto, (max-width: 1622px) 100vw, 1622px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Click the <strong>Shell<\/strong> option. This will take you to the next screen of the <strong>Connect<\/strong> dialog box, as shown in the following figure. Here you\u2019ll find the connection string you need to connect to your Atlas cluster from mongosh. You should be able to stick with the default options already selected, in which case, you need only copy the connection string to your clipboard.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1612\" height=\"1620\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-3.png\" alt=\"\" class=\"wp-image-105826\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-3.png 1612w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-3-300x300.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-3-1019x1024.png 1019w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-3-150x150.png 150w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-3-768x772.png 768w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-3-1528x1536.png 1528w\" sizes=\"auto, (max-width: 1612px) 100vw, 1612px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>The connection string shown in the figure is specific to my Atlas cluster environment, as indicated by the <code>clust1.cb0m704<\/code> portion of the string. I\u2019ve copied the entire connection string here so you can more easily see it:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mongosh \"mongodb+srv:\/\/clust1.cb0m704.mongodb.net\/\" --apiVersion 1 --username &lt;db_username&gt;<\/pre>\n\n\n\n<p>The connection string specifies that the API version should be <code>1<\/code> and provides a placeholder for the <code>--username<\/code> option value. For the exercises in this article, I\u2019m using the <code>user1<\/code> account. You should use whatever account you have set up, as long as it has read and write access to the MongoDB data.<\/p>\n\n\n\n<p>The connection string you copied from the <strong>Connect<\/strong> dialog box is specific to your Atlas cluster environment. As expected, you\u2019ll need to replace the <code>&lt;db_username&gt;<\/code> placeholder with the appropriate username. You\u2019ll also need to know the account\u2019s password. You\u2019ll be prompted for this when you connect to Atlas from mongosh.<\/p>\n\n\n\n<p>Once you\u2019ve prepared your connection string, open your system\u2019s CLI, paste in the connection string at the command prompt, and press Enter. When prompted, enter the password. This should launch mongosh and connect you to your Atlas environment. The following figure shows Terminal on my Mac computer, after I connected to Atlas.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1526\" height=\"676\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-4.png\" alt=\"\" class=\"wp-image-105827\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-4.png 1526w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-4-300x133.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-4-1024x454.png 1024w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-4-768x340.png 768w\" sizes=\"auto, (max-width: 1526px) 100vw, 1526px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Notice that the command prompt now shows details specific to your Atlas connection, along with the name of the current database, which is <code>test<\/code>. This displayed database name refers to the database context in which you\u2019re currently operating. The actual database might not yet exist.<\/p>\n\n\n\n<p>Note that you can also include your password directly in your connection string. To do so, simply add the <code>--password<\/code> argument to your <code>mongosh<\/code> command, as shown in the following syntax:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mongosh \"mongodb+srv:\/\/clust1.cb0m704.mongodb.net\/\" --apiVersion 1 --username &lt;db_username&gt; --password &lt;password&gt;<\/pre>\n\n\n\n<p>Be sure to replace the <code>&lt;password&gt;<\/code> placeholder with your account\u2019s password.<\/p>\n\n\n\n<p>That\u2019s all there is to launching mongosh and connecting to Atlas. You can now interact with your data, as you\u2019ll see shortly. (Note that, if you\u2019re connecting to a local instance of MongoDB, you need only specify <code>mongosh<\/code> for your connection command, without any arguments.)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-running-commands-in-mongosh\">Running commands in mongosh<\/h2>\n\n\n\n<p>If you\u2019ve been following along with this series, you already know how to run commands in mongosh, even if you\u2019ve been using the version embedded in Compass. The process of running commands is the same in either environment. You type or paste the command and press Enter.<\/p>\n\n\n\n<p>For example, to return a list of the current databases in your Atlas cluster, you can run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">show dbs<\/pre>\n\n\n\n<p>When you press Enter, MongoDB will return a list of your current databases, as shown in the following figure. (The exact databases you\u2019ll see will depend on which ones already exist in your Atlas environment.)<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1526\" height=\"956\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-5.png\" alt=\"\" class=\"wp-image-105828\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-5.png 1526w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-5-300x188.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-5-1024x642.png 1024w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-5-768x481.png 768w\" sizes=\"auto, (max-width: 1526px) 100vw, 1526px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>After MongoDB processes a command and displays the results, you\u2019re returned to the command prompt, where you can enter your next command.<\/p>\n\n\n\n<p>Let\u2019s take a look at a few more examples that you can try to help you get comfortable with running commands. We\u2019ll start with the following <code>use<\/code> command, which changes the database context to <code>db1<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">use db1<\/pre>\n\n\n\n<p>When you run the command, you should receive a message stating that you\u2019ve switched to the <code>db1<\/code> database. The command prompt should also show that the <code>db1<\/code> database is now active. If the database does not exist, MongoDB will create it when you add a collection or data, such as in the following <code>insertMany<\/code> command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">db.col1.insertMany([\n  { \"_id\": 101, \"name\": \"Harper\" },\n  { \"_id\": 102, \"name\": \"Avery\" }\n])<\/pre>\n\n\n\n<p>The command adds two documents to the <code>col1<\/code> collection. The <code>db<\/code> at the beginning of the command is a system variable that references the current database object. The variable is followed by the <code>col1<\/code> collection object. If the collection does not exist, MongoDB will create it. The collection object is then followed by <code>insertMany<\/code>, a method available to the object for adding documents to the specified collection.<\/p>\n\n\n\n<p>When you run the command, MongoDB will create the collection in the <code>db1<\/code> database and insert the documents into the collection. MongoDB will then return the following message:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ acknowledged: true, insertedIds: { '0': 101, '1': 102 } }<\/pre>\n\n\n\n<p>You can verify that the documents have been added to the collection by running the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">db.col1.find()<\/pre>\n\n\n\n<p>The command uses the <code>find<\/code> method to return all documents in the collection. When you run the command, you should receive the following results:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[ { _id: 101, name: 'Harper' }, { _id: 102, name: 'Avery' } ]<\/pre>\n\n\n\n<p>Now let\u2019s take a look at how to delete one of the documents. The following command uses the <code>deleteOne<\/code> method to remove the document with an <code>_id<\/code> value of <code>101<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">db.col1.deleteOne( { \"_id\": 101 } )<\/pre>\n\n\n\n<p>When you run the command, MongoDB should return a message stating that a document has been deleted. You can confirm this by again running a <code>find<\/code> command. This time the returned results should include only one document.<\/p>\n\n\n\n<p>There are, of course, many other types of commands you can run at a mongosh command prompt, including aggregations. You can find numerous examples of shell commands earlier in this series, in MongoDB documentation, and in many third-party online resources.<\/p>\n\n\n\n<p>At any point, you can quit the mongosh environment by running the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">quit<\/pre>\n\n\n\n<p>When you run the command, you\u2019ll exit from mongosh and return to your CLI\u2019s regular command prompt. Whenever you want to return to the mongosh environment, simply run your <code>mongosh<\/code> connection command.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-configuring-the-mongosh-environment\">Configuring the mongosh environment<\/h2>\n\n\n\n<p>One of the advantages of using the mongosh standalone version is that you can configure different settings to meet your specific needs, something you cannot do in the Compass embedded version.<\/p>\n\n\n\n<p>MongoDB Shell provides two methods for viewing and configuring settings. The first is to use the <code>config<\/code> API, which is built into the mongosh environment, and the second is to deploy a configuration file that mongosh can read upon startup.<\/p>\n\n\n\n<p>The API approach requires the use of a <code>config<\/code> command, which provides several methods for carrying out different tasks. You can also use the command without specifying a method, in which case, MongoDB will return a list of all the available settings and their current values. To run the command, simply type <strong>config<\/strong> at the mongosh command prompt and press enter:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">config<\/pre>\n\n\n\n<p>The following figure shows the results returned by the <code>config<\/code> command on my system. At this point, I have not modified any of the settings, so the values shown here are the default values.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1522\" height=\"878\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-6.png\" alt=\"\" class=\"wp-image-105829\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-6.png 1522w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-6-300x173.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-6-1024x591.png 1024w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-6-768x443.png 768w\" sizes=\"auto, (max-width: 1522px) 100vw, 1522px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>You can find information about the settings in the MongoDB topic <a href=\"https:\/\/www.mongodb.com\/docs\/mongodb-shell\/reference\/configure-shell-settings\/\">Configure Settings<\/a>, which provides a description of each setting, along with its data type and default value.<\/p>\n\n\n\n<p>In some cases, you might want to retrieve the current value of a specific setting. For this, you can use the <code>get<\/code> method as part of your <code>config<\/code> command, specifying the setting name as an argument to the method:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">config.get(\"showStackTraces\")<\/pre>\n\n\n\n<p>In this case, the command retrieves the current value for the <code>showStackTraces<\/code> setting, which determines how a stack trace and its error messages are displayed. Because the setting is still configured with its default value, the command will return <code>false<\/code>. The setting\u2019s data type is <code>Boolean<\/code>, so its value can be only <code>true<\/code> or <code>false<\/code>.<\/p>\n\n\n\n<p>You can also use <code>config<\/code> command to update a setting\u2019s value. For this, you need to specify the <code>set<\/code> method and then provide the setting name and its new value, as in the following example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">config.set(\"showStackTraces\", true)<\/pre>\n\n\n\n<p>The command sets the <code>showStackTraces<\/code> setting to <code>true<\/code>, which will cause MongoDB to return more detailed stack and error messages. When you run the command, you should receive a message indicating that the setting has been changed. You can confirm your changes by again running a <code>config.<\/code> <code>get(\"showStackTraces\")<\/code> command, which should now return a value of <code>true<\/code>.<\/p>\n\n\n\n<p>After you modify a setting\u2019s value, you might want to return it to its default value. For this, you can use the <code>reset<\/code> method, as shown in the following <code>config<\/code> command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">config.reset(\"showStackTraces\")<\/pre>\n\n\n\n<p>The command should return a message stating the setting has been reset to its default value. You can confirm this by once again running a <code>config.<\/code> <code>get(\"showStackTraces\")<\/code> command, which should now return a value of <code>false<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-creating-a-mongosh-configuration-file\">Creating a mongosh configuration file<\/h2>\n\n\n\n<p>The <code>config<\/code> API provides a straightforward method for viewing and modifying your configuration settings. In some cases, however, you might want to save your settings to a configuration file so the settings are applied to mongosh each time you start it up.<\/p>\n\n\n\n<p>Before I explain how to do that, it\u2019s important to understand how mongosh treats API settings compared to those defined in a configuration file. Settings configured through the API always take precedence over those defined in a configuration file, even if you restart mongosh. Once you use the API to change a setting, the configuration file will have no impact on that setting.<\/p>\n\n\n\n<p>With this in mind, let\u2019s look at an example of how to implement a configuration file. First, create a text file and name it <strong>mongosh.conf<\/strong>. You can then add the settings, which should be written in the YAML format and organized under the <code>mongosh<\/code> namespace. For example, the following code sets the <code>snippetAutoload<\/code> setting to <code>false<\/code> and the <code>inspectDepth<\/code> setting to <code>10<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mongosh:\n  snippetAutoload: false\n  inspectDepth: 10<\/pre>\n\n\n\n<p>After you add your settings to the configuration file, you need to save the file to a location that mongosh can access. The exact location depends on the operating system:<\/p>\n\n\n<div class=\"block-core-list\">\n<ul class=\"wp-block-list\">\n<li>In Windows, save the file to the same directory as the <strong>mongosh.exe<\/strong> binary.<\/li>\n\n\n\n<li>In macOS, save the file to the <strong>\/etc\/<\/strong>, <strong>\/usr\/local\/etc\/<\/strong>, or <strong>\/opt\/homebrew\/etc\/<\/strong> folder.<\/li>\n\n\n\n<li>In Linux, save the file to the <strong>\/etc\/<\/strong> folder.<\/li>\n<\/ul>\n<\/div>\n\n\n<p>Once the file is in place, you need to quit mongosh if you\u2019re still connected and relaunch the program. Next, rerun the <code>config<\/code> command. Your settings should now reflect the values you specified in the configuration file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-configuring-the-mongosh-prompt\">Configuring the mongosh prompt<\/h2>\n\n\n\n<p>You might also find it beneficial to change the default command prompt in mongosh. This can be especially useful when connecting to Atlas, which displays a rather lengthy prompt.<\/p>\n\n\n\n<p>To change the prompt for the current session, set the <code>prompt<\/code> property to the desired value. For example, the following command sets the property to <code>%&gt; <\/code>(space included):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">prompt = \"%&gt; \"<\/pre>\n\n\n\n<p>After you run the command, the command prompt should reflect the new setting, as shown in the following figure. You can also run a <code>show<\/code> <code>dbs<\/code> command to get a better sense of how the new command prompt looks.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1396\" height=\"588\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-7.png\" alt=\"\" class=\"wp-image-105830\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-7.png 1396w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-7-300x126.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-7-1024x431.png 1024w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-7-768x323.png 768w\" sizes=\"auto, (max-width: 1396px) 100vw, 1396px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Users might prefer a simpler prompt such as this, and they can change the prompt in other ways as well. For example, the following command uses the <code>getName<\/code> method to retrieve the name of the current database, which is then concatenated with a right arrow and space:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">prompt = db.getName() + \"&gt; \"<\/pre>\n\n\n\n<p>Now the prompt will display the database name, followed by the arrow and space, as shown in the following figure.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1250\" height=\"592\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-8.png\" alt=\"\" class=\"wp-image-105831\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-8.png 1250w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-8-300x142.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-8-1024x485.png 1024w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-8-768x364.png 768w\" sizes=\"auto, (max-width: 1250px) 100vw, 1250px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Again, this is a much cleaner prompt than the default one you get with an Atlas connection. In fact, this is the same prompt you get by default if you connect to a local instance of MongoDB.<\/p>\n\n\n\n<p>You can also create more complex prompts, depending on your preferences. For instance, the following command uses JavaScript code to create a prompt made up of the line number followed by a right arrow and space:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">let count = 1;\nprompt = function() {\n  return (count++) + \"&gt; \";\n}<\/pre>\n\n\n\n<p>The code begins with a <code>let<\/code> statement that sets the <code>count<\/code> variable to <code>1<\/code>. The code then defines the <code>prompt<\/code> property value, which is a function that increments the <code>count<\/code> variable by <code>1<\/code> with each new line. The variable\u2019s value is then concatenated with a right arrow and space. The following figure show what the new prompt now looks like.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1082\" height=\"814\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-9.png\" alt=\"\" class=\"wp-image-105832\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-9.png 1082w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-9-300x226.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-9-1024x770.png 1024w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105823-9-768x578.png 768w\" sizes=\"auto, (max-width: 1082px) 100vw, 1082px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>You can make all sorts of changes to the <code>prompt<\/code> property, but as already noted, your changes persist only during your current session. However, you can persist your changes across sessions by creating a JavaScript file name <strong>.mongoshrc.js<\/strong> and saving it in to your home directory.<\/p>\n\n\n\n<p>In the file, you should include a JavaScript command that defines the <code>prompt<\/code> property, just like you saw in the previous examples. After you create the file, you\u2019ll need to restart mongosh for your changes to take effect.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-running-shell-scripts-in-mongodb-shell\">Running shell scripts in MongoDB Shell<\/h2>\n\n\n\n<p>Another advantage of using the mongosh standalone version, rather than the Compass embedded version, is that you can run JavaScript files from the command prompt. To demonstrate how this works, start by creating a JavaScript text file named <strong>db2_data.js<\/strong> and then add the following code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">db = connect(\"mongodb+srv:\/\/&lt;user&gt;:&lt;password&gt;@clust1.cb0m704.mongodb.net\/db2\")\ndb.col2.insertMany([\n  { \"_id\": 101, \n    \"name\": \"Drew\",\n    \"position\": { \"title\": \"Senior Developer\", \"dept\": \"R&amp;D\" },\n    \"yrs_exp\": 18,\n    \"skills\": [ \"Java\", \"SQL\", \"Python\", \"PHP\" ] },\n  { \"_id\": 102, \n    \"name\": \"Parker\",\n    \"position\": { \"title\": \"Data Scientist\", \"dept\": \"R&amp;D\" },\n    \"yrs_exp\": 14,\n    \"skills\": [ \"Python\", \"SQL\", \"R\", \"Scala\", \"Java\", \"Julia\" ] },\n  { \"_id\": 103, \n    \"name\": \"Harper\", \n    \"position\": { \"title\": \"Marketing Manager\", \"dept\": \"Marketing\" },\n    \"yrs_exp\": 22 },\n  { \"_id\": 104, \n    \"name\": \"Darcy\",\n    \"position\": { \"title\": \"Senior Developer\", \"dept\": \"R&amp;D\" },\n    \"yrs_exp\": 6,\n    \"skills\": [ \"Java\", \"Csharp\", \"Python\", \"R\" ] },\n  { \"_id\": 105, \n    \"name\": \"Carey\", \n    \"position\": { \"title\": \"SEO Specialist\", \"dept\": \"Marketing\" },\n    \"yrs_exp\": 7 }\n]);<\/pre>\n\n\n\n<p>The code begins with a connection string that connects to the <code>db2<\/code> database on Atlas. Be sure to replace the <code>&lt;user&gt;<\/code> and <code>&lt;password&gt;<\/code> placeholders with the correct account information.<\/p>\n\n\n\n<p>The connection string should also be specific to your Atlas cluster. For example, the <code>clust1.cb0m704<\/code> portion of the connection string is specific to my cluster environment, so you need to replace this portion with the correct information for your environment. The cluster information is the same as what you used for your initial mongosh connection to Atlas.<\/p>\n\n\n\n<p>The remaining part of the code is an <code>insertMany<\/code> statement that inserts five documents into the <code>col2<\/code> collection in the <code>db2<\/code> database.<\/p>\n\n\n\n<p>After you add the code to your <strong>db2_data.js<\/strong> file, you can save it to your system\u2019s home directory or another directory. You can then run script at your convenience, either within the mongosh shell or from your system\u2019s command prompt. To run the script within mongosh, use the <code>load<\/code> method and specify the script file as the method\u2019s argument, as in the following example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">load(\"db2_data.js\")<\/pre>\n\n\n\n<p>If you saved the script file to a location other than your home directory, you must specify the file path, either the relative or absolute path. MongoDB will return <code>true<\/code> if the command runs successfully, otherwise it will return an error message.<\/p>\n\n\n\n<p>In this case, you can also verify whether the script was successful by running the following <code>find<\/code> command, which should return the five documents you just added:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">db.col2.find()<\/pre>\n\n\n\n<p>The mongosh program also lets you run a script file from your system\u2019s CLI command prompt. Before I demonstrate how this works, you should remove the five documents from the collection, just to keep things simple. To remove them, run the following <code>deleteMany<\/code> command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">db.col2.deleteMany({})<\/pre>\n\n\n\n<p>The command should return a message that confirms the deletions. You should then quit mongosh, but leave your CLI open. You can now run the script using a <code>mongosh<\/code> command. At the CLI\u2019s command prompt, run the following <code>mongosh<\/code> command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mongosh --file db2_data.js<\/pre>\n\n\n\n<p>The command includes the <code>--file<\/code> argument, which specifies the <strong>db2_data.js<\/strong> file. Again, if the file is stored somewhere other than the home directory, you must specify the relative or absolute file path. When you execute this command, MongoDB will run the script, although it will not return a message unless there\u2019s an error.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-getting-started-with-mongodb-shell\">Getting started with MongoDB Shell<\/h2>\n\n\n\n<p>As you gain more experience working with MongoDB, whether using Atlas or a local instance, you likely find yourself relying more and more on the freestanding version of mongosh. It provides greater flexibility than the Compass embedded version and is often more expedient when carrying out data-related operations. Be sure to check out the topic <a href=\"https:\/\/www.mongodb.com\/docs\/mongodb-shell\/\">Welcome to MongoDB Shell (mongosh)<\/a> for more detail abouts working in the shell environment. The more familiar you become with using mongosh, the sooner it will become second nature and the more efficiently you\u2019ll be working with MongoDB data.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Throughout this series, I\u2019ve provided a number of examples of how to run commands in MongoDB Shell (mongosh). However, the examples were limited to the version of mongosh that\u2019s embedded in MongoDB Compass. Although mongosh can be a handy tool to use in conjunction with Compass, MongoDB also offers a freestanding version of mongosh that&#8230;&hellip;<\/p>\n","protected":false},"author":221841,"featured_media":105837,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[53,159161],"tags":[5618,159226],"coauthors":[6779],"class_list":["post-105823","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-featured","category-mongodb","tag-mongodb","tag-mongodbseriesrobertsheldon"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/105823","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/221841"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=105823"}],"version-history":[{"count":1,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/105823\/revisions"}],"predecessor-version":[{"id":105836,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/105823\/revisions\/105836"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media\/105837"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=105823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=105823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=105823"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=105823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}