MongoDB Shell
MongoDB Shell is an official interactive interface provided by MongoDB that allows users to interact with MongoDB databases, execute commands, and operate on databases.
MongoDB Shell is based on JavaScript, allowing users to directly use the JavaScript language in the command line or scripts to operate MongoDB databases.
Install MongoDB Shell
MongoDB Shell is part of the MongoDB database installation package; therefore, installing the MongoDB database also automatically installs MongoDB Shell.
Install MongoDB Shell on macOS
Install using Homebrew:
If you use Homebrew to manage software packages on macOS, you can install MongoDB with the following command:
brew tap mongodb/brew
brew install mongodb-community-shell
This will install the latest version of MongoDB and its related tools, including MongoDB Shell.
** Manual download and installation:**
Another method is to download MongoDB Shell for macOS from the MongoDB official website, download address https://www.mongodb.com/try/download/shell :

After downloading, extract the installation package and copy the mongosh binary file in the bin directory to a directory listed in the PATH variable, such as /usr/local/bin:
sudo cp mongosh /usr/local/bin/
sudo cp mongosh_crypt_v1.so /usr/local/lib/
After installation, MongoDB Shell can be launched from the command line with the mongosh command.
If macOS blocks mongosh from running, open "Security & Privacy" in "System Preferences" to allow it to run.
Install MongoDB Shell on Linux
In most Linux distributions, MongoDB Shell can be installed through a package manager. For example, on Ubuntu you can use the apt package manager:
Create the /etc/apt/sources.list.d/mongodb-org-7.0.list file for Ubuntu 22.04 (Jammy):
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
sudo apt update
sudo apt install mongodb-shell
After installation, MongoDB Shell can be launched from the command line with the mongosh command.
Another method is to download MongoDB Shell for Linux from the MongoDB official website, download address https://www.mongodb.com/try/download/shell :

After downloading, extract the installation package and copy the mongosh binary file in the bin directory to a directory listed in the PATH variable, such as /usr/local/bin:
sudo cp mongosh /usr/local/bin/
sudo cp mongosh_crypt_v1.so /usr/local/lib/
After installation, MongoDB Shell can be launched from the command line with the mongosh command.
Install MongoDB Shell on Windows
Visit the MongoDB official download page (https://www.mongodb.com/try/download/shell), select the MongoDB Shell version suitable for Windows, and download the installer.

Run the downloaded installer and follow the instructions to install. During installation, make sure to select MongoDB Shell for installation (it is included by default).
You can choose to configure environment variables during installation, so that you can directly run the mongosh command on the command line to start MongoDB Shell.
Using MongoDB Shell
After installation, you can use MongoDB Shell to connect to a MongoDB database and perform operations through the following steps.
** Start MongoDB Shell:**
Enter the mongosh command on the command line to start MongoDB Shell. If the MongoDB server is running on the local default port (27017), you can connect directly.
mongosh
View version:
mongosh --version
2.2.9
** Connect to MongoDB server:**
If the MongoDB server is running on a non-default port or a remote server, you can connect using the following command:
mongosh --host :
where is the hostname or IP address of the MongoDB server, is the port number of the MongoDB server.
Perform basic operations:
After a successful connection, you can perform various MongoDB database operations. For example:
- View the current database:
db - Display the database list:
show dbs - Switch to a specified database:
use - Perform a query operation:
db..find() - Insert a document:
db..insertOne({ ... }) - Update a document:
db..updateOne({ ... }) - Delete a document:
db..deleteOne({ ... }) - Exit MongoDB Shell:
quit()orexit
Example
The following is an example of using MongoDB Shell to connect to a local MongoDB server and perform some basic operations:
Example
Start MongoDB Shell
mongosh
Connect to the local MongoDB server
test
show dbs
admin
40.00
KiB
config
72.00
KiB
local
40.00
KiB
runoob
72.00
KiB
test
use runoob
switched to db runoob
Insert documents
runoob
db.
mycollection
.
insertOne
(
{
name
:
"Alice"
,
age
:
30
}
)
{
acknowledged
:
true
,
insertedId
:
ObjectId
(
'667cd8789a69705686ed70f2'
)
}
Query documents
runoob
db.
mycollection
.
find
(
)
[
{
_id
:
ObjectId
(
'667cd8789a69705686ed70f2'
)
,
name
:
'Alice'
,
age
:
30
}
]
Update documents
runoob
db.
mycollection
.
updateOne
(
{
name
:
"Alice"
}
,
{
$set
:
{
age
:
31
}
}
)
{
acknowledged
:
true
,
insertedId
:
null
,
matchedCount
:
1
,
modifiedCount
:
1
,
upsertedCount
:
0
}
Delete documents
runoob
db.
mycollection
.
deleteOne
(
{
name
:
"Alice"
}
)
{
acknowledged
:
true
,
deletedCount
:
1
}
Exit MongoDB Shell
runoob
quit
(
)
Through the above steps and examples, you can start using MongoDB Shell to manage and operate MongoDB databases.
Official installation instructions:https://www.mongodb.com/zh-cn/docs/mongodb-shell/install/