Mongo Shell and Commands

Level : Beginner
Mentor: Shailendra Chauhan
Duration : 00:03:00

What is Mongo Shell?

Mongo Shell is a JavaScript-based interactive interface for MongoDB. It can execute any MongoDB-related query or command. After configuring the MongoDB environment variables, use the command Mongo to launch Mongo Shell.

Features of Mongo Shell

  • Interactive JavaScript Environment: Use JavaScript to interact with databases, collections, and documents.
  • CRUD Operations: You can create, read, update, and delete data directly from the shell.
  • Aggregation Framework Support: Execute complicated data aggregation processes.
  • Index Management: Create, drop, and maintain indexes to improve query performance.
  • Administration Commands: Control instances, databases, and collections.
  • JavaScript Expressions: Perform complex data manipulation and scripting.
  • Scripting support: Create JavaScript scripts for automation and batch processing.
  • Data Import and Export: You can import and export data in numerous formats using utility commands.
  • Authentication and authorization: Use authentication procedures to ensure secure access.
  • Replica Set and Sharding Operations: Manage replica sets and sharded clusters.
  • Session Management: Keep sessions for many commands without re-authenticating.
  • Tab completion & syntax highlighting: Improve the user experience and productivity.

Mongo Shell Commands

Start-up and Connection Commands

  • mongod: Starts a local MongoDB server.
  • mongo: Starts the MongoDB shell and connects to the default database, 'test'.
  • Mongo <host_name>:<port>/<database_name> -U <username> -p <password> connects to a MongoDB server with authentication.
  • mongo -version: Shows the version of the MongoDB shell.
  • Mongo --nodb: Starts the MongoDB shell without connecting to a database.
  • mongo <db_name>: Starts the MongoDB shell & connects to a specific database.

Basic Shell Commands

  • help: Displays help for the current context or the MongoDB shell.
  • Exit: Closes the MongoDB shell.
  • db: Displays the currently selected database.
  • Use <db_name>: Switches to and utilizes the specified database.
  • show dbs: This command lists all available databases on the MongoDB server.
  • Show collections: Shows all collections in the current database.
  • Show users: Lists all of the users in the current database.

Document Commands

  • db.users.insert: Adds a document to the 'users' collection.
  • db.users.find: Returns documents from the 'users' collection depending on the query criteria.
  • db.users.find().pretty: Returns documents from the 'users' collection in formatted output.
  • db.users.findOne: Returns a single document from the 'users' collection based on the query parameters.
  • db.users.find({ },{ name:true, _id:false }): Returns particular fields from documents in the 'users' collection.

Query operators

  • db.users.find({"_id": {$gt: 1}}): Returns documents in the 'users' collection with a _id value greater than 1. 
  • db.users.find({"_id": {$gte: 5}}): Returns documents in the 'users' collection with a _id value higher than or equal to 5. 
  • db.users.find({"_id": {$lt: 1}}): Returns documents in the 'users' collection with a _id value less than 1. 
  • db.users.find({"_id": {$lte: 5}}): Returns documents in the 'users' collection with a _id attribute less than or equal to 5. 
  • db.users.find({"_id": {$in: [1,2,3]}}): Identifies documents in the 'users' collection whose _id attribute matches any value in the specified array.
Self-paced Membership
  • 24+ Video Courses
  • 825+ Hands-On Labs
  • 400+ Quick Notes
  • 125+ Skill Tests
  • 10+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this