Mongo shell on windows

Install MongoDB Community on Windows using msiexec.exe В¶

MongoDB Atlas is a hosted MongoDB service option in the cloud which requires no installation overhead and offers a free tier to get started.

OverviewВ¶

Use this tutorial to install MongoDB 4.4 Community Edition on Windows in an unattended fashion using msiexec.exe from the command line. This is useful for system administrators who wish to deploy MongoDB using automation.

MongoDB VersionВ¶

This tutorial installs MongoDB 4.4 Community Edition. To install a different version of MongoDB Community , use the version drop-down menu in the upper-left corner of this page to select the documentation for that version.

Installation MethodВ¶

This tutorial installs MongoDB on Windows using the command-line tool msiexec.exe . To install MongoDB using the graphical MSI Installer instead, see Install MongoDB using the MSI Installer.

ConsiderationsВ¶

Platform SupportВ¶

  • MongoDB 4.4 Community Edition removes support for Windows 8.1 / Server 2012 R2
  • MongoDB 4.4 Community Edition removes support for Windows 8 / Server 2012
  • MongoDB 4.4 Community Edition removes support for Windows 7 / Server 2008 R2

MongoDB 4.4 Community Edition supports the following 64-bit versions of Windows on x86_64 architecture:

  • Windows Server 2019
  • Windows 10 / Windows Server 2016

MongoDB only supports the 64-bit versions of these platforms.

See Supported Platforms for more information.

Production NotesВ¶

Before deploying MongoDB in a production environment, consider the Production Notes document which offers performance considerations and configuration recommendations for production MongoDB deployments.

Install MongoDB Community EditionВ¶

ProcedureВ¶

Follow these steps to install MongoDB Community Edition unattended on Windows from the Windows command prompt/interpreter ( cmd.exe ) using msiexec.exe .

Download the installer.В¶

Download the MongoDB Community .msi installer from the following link:

  1. In the Version dropdown, select the version of MongoDB to download.
  2. In the Platform dropdown, select Windows .
  3. In the Package dropdown, select msi .
  4. Click Download .

The mongo ShellВ¶

The mongo shell is an interactive JavaScript interface to MongoDB. You can use the mongo shell to query and update data as well as perform administrative operations.

The following document pertains to the mongo shell included in the MongoDB Server Download. For information on the new MongoDB Shell, mongosh , refer to the mongosh Documentation.

To understand the differences between the two shells, see Comparison of the mongo Shell and mongosh .

Download the mongo ShellВ¶

The mongo shell is included as part of the MongoDB server installation. If you have already installed the server, the mongo shell is installed to the same location as the server binary.

Читайте также:  Автоматическое обновление windows как поставить

Alternatively, if you would like to download the mongo shell separately from the MongoDB Server, you can install the shell as a standalone package by following these steps:

Access the Download Center for your Edition of MongoDB:

Select the Package to download according to your platform:

For additional installation guidance specific to your platform, or to install the mongo shell as part of a MongoDB Server installation, see the installation guide for your platform.

Start the mongo Shell and Connect to MongoDBВ¶

Once you have downloaded the mongo shell, you can use it to connect to your running MongoDB server.

Starting in MongoDB 4.2 (and 4.0.13), the mongo shell displays a warning message when connected to non-genuine MongoDB instances as these instances may behave differently from the official MongoDB instances; e.g. missing or incomplete features, different feature behaviors, etc.

PrerequisitesВ¶

    The MongoDB server must be installed and running before you can connect to it from the mongo shell. Follow the steps in the installation tutorial for your platform to install and start the MongoDB server if required.

Once you have verified that the mongod server is running, open a terminal window (or a command prompt for Windows) and go to your directory:

Adding your to the PATH environment variable allows you to type mongo directly instead of having to first go to the directory or specify the full path to the binary. Alternatively, you can copy the mongo shell to a location on your filesystem that is already present in your PATH , such as /usr/bin on Linux.

macOS Security VerificationВ¶

For macOS users:

macOS may prevent the mongo shell from running after installation. If you receive a security error when starting the mongo shell indicating that the developer could not be identified or verified, do the following to grant the mongo shell access to run:

  • Open System Preferences
  • Select the Security and Privacy pane.
  • Under the General tab, click the button to the right of the message about the mongo shell, labelled either Open Anyway or Allow Anyway depending on your version of macOS.

Local MongoDB Instance on Default PortВ¶

You can run mongo shell without any command-line options to connect to a MongoDB instance running on your localhost with default port 27017:

Local MongoDB Instance on a Non-default PortВ¶

To explicitly specify the port, include the —port command-line option. For example, to connect to a MongoDB instance running on localhost with a non-default port 28015:

MongoDB Instance on a Remote HostВ¶

To explicitly specify the hostname and/or port,

You can specify a connection string. For example, to connect to a MongoDB instance running on a remote host machine:

You can use the command-line option —host :

. For example, to connect to a MongoDB instance running on a remote host machine:

You can use the —host and —port

command-line options. For example, to connect to a MongoDB instance running on a remote host machine:

MongoDB Instance with AuthenticationВ¶

To connect to a MongoDB instance requires authentication:

You can specify the username, authentication database, and optionally the password in the connection string. For example, to connect and authenticate to a remote MongoDB instance as user alice :

Читайте также:  Удаляют windows 10 шпионская

If you do not specify the password in the connection string, the shell will prompt for the password.

You can use the —username and —password , —authenticationDatabase command-line options. For example, to connect and authenticate to a remote MongoDB instance as user alice :

If you specify —password without the user’s password, the shell will prompt for the password.

Connect to a MongoDB Replica SetВ¶

To connect to a replica set:

You can specify the replica set name and members in the connection string.

If using the DNS Seed List Connection Format, you can specify the connection string:

Use of the +srv connection string modifier automatically sets the ssl option to true for the connection.

You can specify the replica set name and members from the —host / :

. command-line option. For example, to connect to replica set named replA :

TLS/SSL ConnectionВ¶

For TLS/SSL connections,

You can specify the ssl=true option in the connection string.

If using the DNS Seed List Connection Format, you can include the +srv connection string modifier:

Use of the +srv connection string modifier automatically sets the ssl option to true for the connection.

You can specify —ssl command-line option. For example, to connect to replica set named replA :

For more information on the options used in the connection examples as well as other options, see mongo reference and examples of starting up mongo.

Working with the mongo ShellВ¶

To display the database you are using, type db :

The operation should return test , which is the default database.

To switch databases, issue the use helper, as in the following example:

See also db.getSiblingDB() method to access a different database from the current database without switching your current database context (i.e. db ).

To list the databases available to the user, use the helper show dbs . [ 1 ]

You can switch to non-existing databases. When you first store data in the database, such as by creating a collection, MongoDB creates the database. For example, the following creates both the database myNewDatabase and the collection myCollection during the insertOne() operation:

  • db refers to the current database.
  • myCollection is the name of the collection.

If the mongo shell does not accept the name of a collection, you can use the alternative db.getCollection() syntax. For instance, if a collection name contains a space or hyphen, starts with a number, or conflicts with a built-in function:

The mongo shell prompt has a limit of 4095 codepoints for each line. If you enter a line with more than 4095 codepoints, the shell will truncate it.

For more documentation of basic MongoDB operations in the mongo shell, see:

  • Getting Started Guide
  • Insert Documents
  • Query Documents
  • Update Documents
  • Delete Documents
  • mongo Shell Methods
[1] If the deployment runs with access control, the operation returns different values based on user privileges. See listDatabases Behavior for details.

Format Printed ResultsВ¶

The db.collection.find() method returns a cursor to the results; however, in the mongo shell, if the returned cursor is not assigned to a variable using the var keyword, then the cursor is automatically iterated up to 20 times to print up to the first 20 documents that match the query. The mongo shell will prompt Type it to iterate another 20 times.

Читайте также:  Interface web windows server

To format the printed result, you can add the .pretty() to the operation, as in the following:

In addition, you can use the following explicit print methods in the mongo shell:

  • print() to print without formatting
  • print(tojson( )) to print with JSON formatting and equivalent to printjson()
  • printjson() to print with JSON formatting and equivalent to print(tojson( ))

For more information and examples on cursor handling in the mongo shell, see Iterate a Cursor in the mongo Shell. See also Cursor Help for list of cursor help in the mongo shell.

Multi-line Operations in the mongo ShellВ¶

If you end a line with an open parenthesis ( ‘(‘ ), an open brace ( ‘<' ), or an open bracket ( '[' ), then the subsequent lines start with ellipsis ( ". " ) until you enter the corresponding closing parenthesis ( ')' ), the closing brace ( '>‘ ) or the closing bracket ( ‘]’ ). The mongo shell waits for the closing parenthesis, closing brace, or the closing bracket before evaluating the code, as in the following example:

You can exit the line continuation mode if you enter two blank lines, as in the following example:

Tab Completion and Other Keyboard ShortcutsВ¶

The mongo shell supports keyboard shortcuts. For example,

    Use the up/down arrow keys to scroll through command history. See .dbshell documentation for more information on the .dbshell file.

Use to autocomplete or to list the completion possibilities, as in the following example which uses to complete the method name starting with the letter ‘c’ :

Because there are many collection methods starting with the letter ‘c’ , the will list the various methods that start with ‘c’ .

For a full list of the shortcuts, see Shell Keyboard Shortcuts

.mongorc.js FileВ¶

When starting, mongo checks the user’s HOME directory for a JavaScript file named .mongorc.js. If found, mongo interprets the content of .mongorc.js before displaying the prompt for the first time. If you use the shell to evaluate a JavaScript file or expression, either by using the —eval option on the command line or by specifying a .js file to mongo, mongo will read the .mongorc.js file after the JavaScript has finished processing. You can prevent .mongorc.js from being loaded by using the —norc option.

Exit the ShellВ¶

To exit the shell, type quit() or use the shortcut.

Comparison of the mongo Shell and mongosh В¶

mongosh is currently available as a Beta release. The product, its features, and the corresponding documentation may change during the Beta stage.

The new MongoDB Shell, mongosh, offers numerous advantages over the mongo shell, such as:

  • Improved syntax highlighting.
  • Improved command history.
  • Improved logging.

During the beta stage, mongosh supports a subset of the mongo shell methods. Achieving feature parity between mongosh and the mongo shell is an ongoing effort.

To maintain backwards compatibility, the methods that mongosh supports use the same syntax as the corresponding methods in the mongo shell. To see the complete list of methods supported by mongosh , see MongoDB Shell Methods.

В© MongoDB, Inc 2008-present. MongoDB, Mongo, and the leaf logo are registered trademarks of MongoDB, Inc.

Оцените статью