SoFunction
Updated on 2025-04-06

Mongo Shell Basic Operations of Execution Environment

Mongo Shell is an interactive JavaScript shell for MongoDB, used to interact with MongoDB databases.

1. Start and connect

1. Startup method

In a system with MongoDB installed, open the command line terminal and enter the `mongo` command to start Mongo Shell. If the executable file path of MongoDB is not in the system environment variable, you need to first enter the bin folder in the MongoDB installation directory, and then execute the mongo command. If the database is not started, enter `mongod` to start the database.

2. Connect to the database

By default, Mongo Shell attempts to connect to a locally running MongoDB instance with port 27017. You can also specify information such as the host, port, and database to connect to at startup, such as `mongo mongodb://localhost:27017/mydb`, where `mydb` is the name of the database to be connected to.

2. Basic operations

1. Database operations

use: Switch or create a database

db: View the currently connected database

show dbs: View all databases

2. Collection operations

('mycollection'): Command to create a collection

show collections: View all collections in the current database

(): increase

(): check

(): change

():delete

3. Data type

1. Supported data types

Mongo Shell supports a variety of data types, including strings, numbers, dates, arrays, objects, etc.

({
  name: "John Doe",
  age: 30,
  hobbies: ["reading", "coding"],
  address: {
    street: "123 Main St",
    city: "Anytown",
  },
});

4. Script writing and execution

1. Write scripts

JavaScript scripts can be written directly in Mongo Shell to implement complex database operation logic.

function findUsersByAge(age) {
  return ({ age: age });
}

2. Execute the script file

You can also save the Mongo Shell script as a `.js` file and then execute the script in the command line using the `mongo <>` method, where `<>` is the path to the script file.

V. Permissions and Authentication

1. Permission Management

MongoDB supports user permission management, and different users have different permissions to access and operate databases. In Mongo Shell, you can use the `()` command to create a user and grant roles and permissions to the user using the `()` command.

2. Authentication method

After MongoDB enables authentication, you need to provide a username and password for authentication when connecting to the database. You can specify a user name and password in the connection string, such as `mongo mongodb://username:password@localhost:27017/mydb`.

6. Interaction with programming languages

1. Interact with

In applications, you can use the `mongodb` module to connect and operate MongoDB databases, and their operation methods have many similarities to Mongo Shell. Through the `mongodb` module, database operations similar to Mongo Shell can be performed in , and data storage, querying and updating functions can be implemented.

2. Interact with Python

In Python, you can use the `pymongo` library to interact with MongoDB. `pymongo` provides operation methods similar to Mongo Shell, making it easy to operate MongoDB databases in Python, such as inserting data, querying data and updating data.

This is all about this article about the Mongo Shell execution environment. For more related Mongo Shell execution environment content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!