How to use the command mongosh (with examples)

How to use the command mongosh (with examples)

mongosh is a new shell for MongoDB and is intended to replace the existing mongo shell. It provides a user-friendly interface for interacting with MongoDB databases. In this article, we will explore different use cases of using the mongosh command.

Use case 1: Connect to a local database on the default port

Code:

mongosh

Motivation:

This use case allows us to connect to a local database on the default port (mongodb://localhost:27017). It is useful when we want to quickly connect to a MongoDB database without specifying any additional parameters.

Explanation:

  • No arguments are provided to the mongosh command.
  • The default connection options are used to connect to the local database.

Example output:

Current Mongosh Log ID: XXXXXXXXXXXXXXXXXX
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000
Using MongoDB:          4.4.6
Using Mongosh:          1.0.5

For mongosh info, press Ctrl+D

Use case 2: Connect to a database

Code:

mongosh --host host --port port db_name

Motivation:

This use case allows us to connect to a specific database on a MongoDB server. It is useful when we want to work with a specific database and know the host and port information of the server.

Explanation:

  • The --host argument is used to specify the hostname or IP address of the MongoDB server.
  • The --port argument is used to specify the port number of the MongoDB server.
  • The db_name argument is used to specify the name of the database to connect to.

Example output:

Current Mongosh Log ID: XXXXXXXXXXXXXXXXXX
Connecting to:          mongodb://host:port/db_name?directConnection=true&serverSelectionTimeoutMS=2000
Using MongoDB:          4.4.6
Using Mongosh:          1.0.5

For mongosh info, press Ctrl+D

Use case 3: Authenticate using the specified username on the specified database

Code:

mongosh --host host --port port --username username --authenticationDatabase authdb_name db_name

Motivation:

This use case allows us to authenticate using a specified username on a specific MongoDB database. It is useful when we want to connect to a database that requires authentication.

Explanation:

  • The --host argument is used to specify the hostname or IP address of the MongoDB server.
  • The --port argument is used to specify the port number of the MongoDB server.
  • The --username argument is used to specify the username for authentication.
  • The --authenticationDatabase argument is used to specify the name of the authentication database.
  • The db_name argument is used to specify the name of the database to connect to.

Example output:

Current Mongosh Log ID: XXXXXXXXXXXXXXXXXX
Connecting to:          mongodb://username@host:port/db_name?authSource=authdb_name&directConnection=true&serverSelectionTimeoutMS=2000
Using MongoDB:          4.4.6
Using Mongosh:          1.0.5
Enter password:
Authentication successful

For mongosh info, press Ctrl+D

Use case 4: Evaluate a JavaScript expression on a database

Code:

mongosh --eval 'JSON.stringify(db.foo.findOne())' db_name

Motivation:

This use case allows us to evaluate a JavaScript expression on a MongoDB database. It is useful when we want to perform complex database operations or retrieve specific data using JavaScript expressions.

Explanation:

  • The --eval argument is used to specify the JavaScript expression to evaluate.
  • The JavaScript expression should be enclosed in single quotes.
  • The db_name argument is used to specify the name of the database to connect to.

Example output:

Current Mongosh Log ID: XXXXXXXXXXXXXXXXXX
Connecting to:          mongodb://127.0.0.1:27017/db_name?directConnection=true&serverSelectionTimeoutMS=2000
Using MongoDB:          4.4.6
Using Mongosh:          1.0.5

{\"_id\": \"XXXXXXXXXXXXXXXX\", \"name\": \"John Doe\", \"age\": 30}

For mongosh info, press Ctrl+D

Conclusion:

The mongosh command provides a flexible and user-friendly way to interact with MongoDB databases. Whether you need to connect to a local or remote database, authenticate with a specific user, or evaluate JavaScript expressions, mongosh has you covered. Use the provided examples to get started with using mongosh for your MongoDB database needs.

Related Posts

How to use the command 'now' (with examples)

How to use the command 'now' (with examples)

The ’now’ command is a cloud platform for serverless deployment. However, it is important to note that this command is deprecated and users are encouraged to use the updated version ‘vercel’ instead.

Read More
How to use the command dumpcap (with examples)

How to use the command dumpcap (with examples)

Dumpcap is a command-line packet capture tool that comes bundled with Wireshark, a widely used network protocol analyzer.

Read More
How to use the command 'nx' (with examples)

How to use the command 'nx' (with examples)

The nx command is used to manage nx workspaces. It provides various functions to build, test, and execute targets on projects within the workspace.

Read More