YouTip LogoYouTip

Mongodb Shell

MongoDB Shell is the official interactive interface provided by MongoDB, allowing users to interact with MongoDB databases, execute commands, and operate databases. MongoDB Shell is JavaScript-based, allowing users to directly use the JavaScript language in the command line or scripts to operate MongoDB databases. * * * ## Installing MongoDB Shell MongoDB Shell is part of the MongoDB database installation package, so installing MongoDB database will also automatically install MongoDB Shell. ### Installing MongoDB Shell on macOS **Using Homebrew to install:** If you use Homebrew to manage packages on macOS, you can install MongoDB with the following commands: 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 the MongoDB Shell for macOS from the MongoDB official website at [https://www.mongodb.com/try/download/shell](https://www.mongodb.com/try/download/shell): !(#) After downloading, extract the installation package and copy the mongosh binary file from 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 is complete, MongoDB Shell can be launched via the mongosh command in the command line. If macOS blocks mongosh from running, open "Security & Privacy" in "System Preferences" to allow it to run. ### Installing MongoDB Shell on Linux On most Linux distributions, MongoDB Shell can be installed via package manager. For example, on Ubuntu you can use the apt package manager: Create /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 is complete, MongoDB Shell can be launched via the mongosh command in the command line. Another method is to download the MongoDB Shell for Linux from the MongoDB official website at [https://www.mongodb.com/try/download/shell](https://www.mongodb.com/try/download/shell): !(#) After downloading, extract the installation package and copy the mongosh binary file from 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 is complete, MongoDB Shell can be launched via the mongosh command in the command line. ### Installing MongoDB Shell on Windows Visit the MongoDB official website download page ([https://www.mongodb.com/try/download/shell](https://www.mongodb.com/try/download/shell)), select the appropriate MongoDB Shell version for Windows, and download the installer. !(#) Run the downloaded installer and follow the instructions to install. During installation, ensure that MongoDB Shell is selected for installation (it will be included by default). You can choose to configure environment variables during installation, so that you can directly run the mongosh command in the command line to start MongoDB Shell. * * * ## Using MongoDB Shell After installation is complete, you can use the following steps to use MongoDB Shell to connect to a MongoDB database and perform operations. **Starting MongoDB Shell:** Enter the mongosh command in the command line to start MongoDB Shell. If the MongoDB server is running on the local default port (27017), it will connect directly. mongosh View version: mongosh --version 2.2.9 **Connecting to MongoDB server:** If the MongoDB server is running on a non-default port or on a remote server, you can use the following command to connect: mongosh --host : Where `` is the hostname or IP address of the MongoDB server, and `` is the port number of the MongoDB server. **Performing basic operations:** After successful connection, you can perform various MongoDB database operations. For example: * View current database: `db` * Show database list: `show dbs` * Switch to specified database: `use ` * Execute query operation: `db..find()` * Insert document: `db..insertOne({ ... })` * Update document: `db..updateOne({ ... })` * Delete document: `db..deleteOne({ ... })` * Exit MongoDB Shell: `quit()` or `exit` ### 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 local MongoDB server test> show dbs admin 40.00 KiB config 72.00 KiB local 40.00 KiB 72.00 KiB test> use switched to db # Insert document > db.mycollection.insertOne({ name:"Alice", age:30}) { acknowledged:true, insertedId: ObjectId('667cd8789a69705686ed70f2') } # Query document > db.mycollection.find() [ { _id: ObjectId('667cd8789a69705686ed70f2'), name:'Alice', age:30} ] # Update document > db.mycollection.updateOne({ name:"Alice"},{ $set:{ age:31}}) { acknowledged:true, insertedId:null, matchedCount:1, modifiedCount:1, upsertedCount:0 } # Delete document > db.mycollection.deleteOne({ name:"Alice"}) { acknowledged:true, deletedCount:1} # Exit MongoDB Shell > 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/](https://www.mongodb.com/zh-cn/docs/mongodb-shell/install/)
← React ConditionalC Extern β†’