10 Most Common Commands in MongoDB


Here we discuss about ten most commonly used commands for MongoDB beginners. Please feel free to comment and make suggestions if I failed to mention any important points!

After installation:

1. Start MongoDB: Open cmd and run mongod

>mongod

Open another cmd and run this commend

>mongo

2. Show All Databases:

>show dbs

3. Select Database to Work With:

>use databaseName

4. Authenticate and Log Out From Database:

// Authenticate

//

>db.auth("username", "password");

//

// Logout

//

>db.logout()

6. Create a Collection:

>db.createCollection("collectionName");

7. Insert a Document in a Collection:

Once a collection is created, the next step is to insert one or more documents. Following is a sample command for inserting a document in a collection.

// Insert single document

//

>db.<collectionName>.insert({field1: "value", field2: "value"})

//

// Insert multiple documents

//

>db.<collectionName>.insert([{field1: "value1"}, {field1: "value2"}])

>db.<collectionName>.insertMany([{field1: "value1"}, {field1: "value2"}])

8. Save or Update Document:

The save command can be used to either update an existing document or insert a new one depending on the document parameter passed to it.

// Matching document will be updated; In case, no document matching the ID is found, a new document is created

//

>db.<collectionName>.save({"_id": new ObjectId("jhgsdjhgdsf"), field1: "value", field2: "value"});

9. Display Collection Records:

The following commands can be used to retrieve collection records:

// Retrieve all records

>db.<collectionName>.find();

// Retrieve limited number of records; Following command will print 10 results;

>db.<collectionName>.find().limit(10);

// Retrieve records by id

>db.<collectionName>.find({"_id": ObjectId("someid")});

// Retrieve values of specific collection attributes by passing an object having

// attribute names assigned to 1 or 0 based on whether that attribute value needs

// to be included in the output or not, respectively.

>db.<collectionName>.find({"_id": ObjectId("someid")}, {field1: 1, field2: 1});

>db.<collectionName>.find({"_id": ObjectId("someid")}, {field1: 0}); // Exclude field1

//

// Collection count

//

>db.<collectionName>.count();

10. Administrative Commands:

Following are some of the administrative commands that can be helpful in finding collection details such as storage size, total size, and overall statistics.

// Get the collection statistics

//

>db.<collectionName>.stats()

>db.printCollectionStats()

//

// Latency statistics for read, writes operations including average time taken for reads, writes

// and related umber of operations performed

//

>db.<collectionName>.latencyStats()

//

// Get collection size for data and indexes

//

>db.<collectionName>.dataSize() // Size of the collection

>db.<collectionName>.storageSize() // Total size of document stored in the collection

>db.<collectionName>.totalSize() // Total size in bytes for both collection data and indexes

>db.<collectionName>.totalIndexSize()// Total size of all indexes in the collection

For more details contact at here

#Howtoimplementmongodbcommand #Top10commandinmongodb #Howtoinstallmongodbcommand #Importantmongodbquery #Crudoperationinmongodb #Basiccommandofmongodb #HowtostartMongoDBincommandprompt #Howtoshowdatainmongodb #Howtoinsertrecordinmongodb #Howtoupdaterecordinmongodb

Contact Us

Tel: (+91) 0120  4118730  

Time :   10 : 00  AM -  08 : 00 PM IST 

Registered address: G-69, Sector 63, 

 Noida - 201301, India

We Provide Services Across The different countries

USA    Australia   Canada   UK    UAE    Singapore   New Zealand    Malasia   India   Ireland   Germany

CodersArts is a Product by Sofstack Technology Solutions Pvt. Ltd.

  • CodersArts | Linkedin
  • Instagram