0% found this document useful (0 votes)
5 views8 pages

PRACTICE+-+5 1+to+5 4+-+Introduction+to+MongoDB+Shell

The document provides an introduction to the MongoDB Shell, detailing how to connect to local and remote MongoDB instances, including those requiring authentication. It also includes instructions for configuring the shell prompt to display various information such as the number of operations, database name, hostname, uptime, and document count. Additionally, it outlines how to access help for commands, databases, collections, and more within the shell.

Uploaded by

Kunal Labde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

PRACTICE+-+5 1+to+5 4+-+Introduction+to+MongoDB+Shell

The document provides an introduction to the MongoDB Shell, detailing how to connect to local and remote MongoDB instances, including those requiring authentication. It also includes instructions for configuring the shell prompt to display various information such as the number of operations, database name, hostname, uptime, and document count. Additionally, it outlines how to access help for commands, databases, collections, and more within the shell.

Uploaded by

Kunal Labde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Introduction to MongoDB

Shell
- Firoj Atar

Introduction to MongoDB Shell 1


MongoDB Shell
• Connect local MongoDB instance on default port
mongosh

• Connect local MongoDB instance on different port


mongosh --port 8883

• Connect Remote MongoDB instance


mongosh --host mongodb0.example.com --port 8883

• Connect MongoDB instance with Authentication


mongosh --host mongodb0.examples.com --port 28015 --username alice --password --
authenticationDatabase admin
Introduction to MongoDB Shell 2
Configuring MongoDB Shell
- Firoj Atar

Introduction to MongoDB Shell 3


Display Number of Operations
• Setup mongo shell prompt to display number of operations issued in the current
session

• Code :
cmdCount = 1;
prompt = function() {
return (cmdCount++) + "> ";
}

• Result
1>
2>
3>

Introduction to MongoDB Shell 4


Display Database and Hostname
• Setup mongo shell prompt in the form of <database>@<hostname>$

• Code
host = db.serverStatus().host;

prompt = function() {
return db+"@"+host+"$ ";
}

• Result
test@myHost1$

Introduction to MongoDB Shell 5


Display Up Time and Document Count
• Setup mongo shell prompt that contains the system up time and the
number of documents in the current database

Codeprompt = function() {
return "Uptime:"+db.serverStatus().uptime+" Documents:"+db.stats().objects+"
> ";
}

• Result
• Uptime:5897 Documents:6 >

Introduction to MongoDB Shell 6


Accessing mongo Shell Help
- Firoj Atar

Introduction to MongoDB Shell 7


• Command-line help
• mongosh –help

• Shell help
• help

• Database help
• db.help()

• Collection help
• db.collection.help()

• Curson help
• db.collection.find().help()

• Wrapper Object help


• help misc

Introduction to MongoDB Shell 8

You might also like