technical skills grow

Responsive Ads Here

Tuesday, December 12, 2023

MongoDB Shell

 An interactive JavaScript interface to MongoDB 

 Used for

 add new data 

 query the existing data

 update the existing data  

remove the data  

perform administrative operations 

Separate Installation 

Can be installed on the same location as the server binary

MongoDB Shell 

Connect local 

MongoDB instanceon default portmongosh 

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 --authenticationDatabaseadmi

Configuring MongoDB Shell 

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++) + "> "

   ;} 

Result1>2>3> 

 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$ 

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 > 

Accessing mongo Shell Help 

Command-line help 
Shell help
Database help 
Collection help
Curson help 
Wrapper Object help
 
Data Types in the mongo Shell

 Date 

  •Date() 
  •new Date() 
  •ISODate()
 
 ObjectId
 new ObjectId 
 
NumberLong 
NumberLong("2090845886852")

NumberInt 
NumberLong("2090845“)
 
NumberDecimal
NumberDecimal("1000.55") or NumberDecimal(1000.55) 
 
 

No comments:

Post a Comment

Powered by Blogger.

Labels

Contact Form

Name

Email *

Message *

Search This Blog

Blog Archive

Ad Code

Responsive Advertisement

Recent Posts