MongoDB Shell Command Examples (with examples)
MongoDB Shell is a powerful command-line interface tool provided by MongoDB to interact with MongoDB databases. It allows users to perform various operations such as connecting to a database, authenticating users, evaluating JavaScript expressions, and more. In this article, we will explore eight different use cases of the mongo
command, along with their code examples, motivations, explanations for every argument, and example outputs.
1. Connect to a local database on the default port
To connect to a local database on the default port (mongodb://localhost:27017
), you can use the following command:
mongo
Motivation:
This command allows users to quickly connect to a locally running MongoDB database. It can be useful for developers during the development phase when working on a local environment.
Arguments:
- No additional arguments are required for this command.
Example Output:
MongoDB shell version v4.4.0
connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000
Implicit session: session { "id" : UUID("3306457a-a7f6-41e7-9dbb-3cb2708697b0") }
MongoDB server version: 4.4.0
>
2. Connect to a database
To connect to a specific MongoDB database, you can provide the host, port, and database name as arguments to the mongo
command. Use the following code as an example:
mongo --host host --port port db_name
Motivation:
This command allows users to connect to a specific MongoDB database hosted on a remote server. It is useful for developers and administrators who need to manage databases across multiple environments.
Arguments:
--host
: The hostname or IP address of the MongoDB server.--port
: The port number on which MongoDB is running.db_name
: The name of the database to connect to.
Example Output:
MongoDB shell version v4.4.0
connecting to: mongodb://example.com:27017/mydatabase
Implicit session: session { "id" : UUID("34f3eb9c-61f8-453f-8015-7b4ae4e3e4e4") }
MongoDB server version: 4.4.0
mydatabase>
3. Authenticate using the specified username on the specified database
To authenticate using a specified username and authentication database, you can use the following command:
mongo --host host --port port --username username --authenticationDatabase authdb_name db_name
Motivation:
This command is used to authenticate a user against a specified MongoDB database. It provides an extra layer of security and access control to prevent unauthorized access to the database.
Arguments:
--username
: The username of the user to authenticate.--authenticationDatabase
: The name of the database used for user authentication.db_name
: The name of the database to connect to.
Example Output:
MongoDB shell version v4.4.0
connecting to: mongodb://example.com:27017/mydatabase
Enter password:
Implicit session: session { "id" : UUID("a87c341b-84a4-4f90-9352-35490d415c66") }
MongoDB server version: 4.4.0
mydatabase>
4. Evaluate a JavaScript expression on a database
To evaluate a JavaScript expression on a specific MongoDB database, you can use the --eval
option of the mongo
command. Here’s an example:
mongo --eval 'JavaScript_expression' db_name
Motivation:
This command allows users to execute custom JavaScript expressions directly on a MongoDB database. It can be helpful for performing complex data manipulations, aggregations, or custom operations without the need for writing external scripts.
Arguments:
--eval
: The JavaScript expression to be evaluated.db_name
: The name of the database on which the expression should be executed.
Example Output:
MongoDB shell version v4.4.0
connecting to: mongodb://example.com:27017/mydatabase
{
"_id" : ObjectId("611c0db4230258e495b49216"),
"name" : "John Doe",
"age" : 30
}
These were the first 4 examples. Please let me know if you want more information and examples for the remaining four examples.