Quick Intro

In this new era of smart technology, data is being generated in high volume and every piece of data is equally important for growing industries. Users are generating structured, semi-structured, and unstructured data in an unlimited amount. Structured Data comprises storing data in tables and rows whereas unstructured data consists of images, videos, and voice clips. Due to increasing data volume of structured and unstructured Data necessity of NoSQL Database comes into the picture. It provides developers ease of designing schema and scalable platform to database administrators, it provides a secure and speedy platform.

What is MongoDB?

MongoDB is a document-oriented, cross-platform and open-source NoSQL Database used to store semi-structured data written in C++. Instead of tables and rows, MongoDB stores data in key-value pairs.  To make learning easy and hustle free for developers and administrators, here are some of the frequently used MongoDB commands. Let’s get it started.

Basic Commands

1. Version check

The foremost command is to check the installed version of the MongoDB server and Mongo Shell. Run this command on the terminal on Linux or CMD prompt on windows. mongod –version We can also use mongod command to check the version, as follows. mongo –version

2. Listing MongoDB commands

This command will help users to find out all the commands which can be used in MongoDB. Run the command on Mongo Shell. help()

3. DB statistics

The below command will give details of databases along with several collections and related parameters of that Database. db.stats()

4. Create New DB or Switch to Existing DB

This simple command help to create a new database if it doesn’t exist or help to switch to the existing Database. In MongoDB “test” is default database hence users use “test” DB once Mongo Shell is logged in. use DB_Name

5. Listing all the Databases

The mentioned command is being used to list all the databases. show dbs

6. Check the DB currently in use

Run below command on Mongo Shell to see the DB currently in use. db

 7. Drop Database

The given command helps the user to drop the required database. Run the command on MongoDB client.  Please make sure to select the Database before running the drop command. Otherwise, it will drop the default “test” Database. db.dropDatabase() Let’s first list out all the database, switch to one of them and then drop it

8. Create Collection

Collections are similar to tables in RDBMS. Create a collection command consists of two parameters. The collection consists of zero or more documents. Hence for creating a collection mandatory parameter to use in command is its name and optional parameter might include the name of documents, its size, and index.

Creating a simple collection.

Syntax: db.createCollection(Name,Options) Example:

Creating a Capped Collection

In this, restrict the size and number of the documents to be inserted into the collection. The capped collection has a property to remove the oldest documents to make space for new documents. Syntax: db.createCollection(Name,{capped : true, size : sizeLimit , max : documentLimit }) Example: Let’s create a capped collection, insert a record and retrieve it

9. Drop Collection

Drop Collection command is similar to DDL in RDBMS. It acquires locks on the required collection until the execution of the command. Drop collection removes the collection from the DB along with all the indexes associated with that collection. To drop the collection drop() method is required. It returns true for successful drop and false in case of any error or if DB doesn’t exist. Syntax: collectionName.drop() Example:

10. Insert Document into Collection

In MongoDB document is similar to a tuple in RDBMS. To create a document, the insert() method is used. The insert() method creates one or many documents in the existing collection. It also creates collection if it is not present in DB.  In MongoDB, Document is schema-less, it means there is no restriction in inserting any number of keys in a document.

Inserting a single record

To insert one record insert() or insertOne() method can be used. Syntax: collectionName.insertOne({document}) Example:

Inserting multiple records

To insert many records, a list of records will be passed to insert() or insertMany() method. Syntax: collectionName.insertMany([{document1},{document2},{ document3}….{ documentn}]) Example:

Inserting record in bulk

A huge number of documents can also be inserted in an ordered and unordered manner by executing initializeOrderedBulkOp() and initializeUnorderedBulkOp() methods. Syntax: Example:

11. Retrieve Document from a Collection

To search for the document stored in a collection find() method can be used. The below command will be used to retrieve all the documents from the collection.

find()method can be used to retrieve all documents stored in a collection.

Syntax: collectionName.find() Example:

find({condition}) method can be used to retrieve only the required documents based on some conditions from the collection. MongoDB provides a list of projection and Query operators to retrieve BSON type value.

Syntax: collectionName.find({ condition }) Example:

To retrieve only one document MongoDB provides the findOne() method. It gives a formatted output.

Syntax: collectionName.findOne() Example:

12. Beautify Retrieval output

The find() method gives a disorganized output.  MongoDB provides pretty() commands to get the formatted output. Syntax: collectionName.find().pretty() Example:

13. Update Document in a Collection

MongoDB provides update() method to set new values for existing keys in documents. Update command gives details of modified and matched documents. Syntax of update command is: Syntax: collectionName.update({KeyToUpdate},{Set Command}) Example:

updateOne() : To update a single document there is updateOne() method. updateOne() give the count of matched and modified documents.

Syntax: collectionName.updateOne({SingleKeyToUpdate},{Set Command}) Example:

updateMany() : To update multiple documents on some condition MongoDB has updateMany() method.

Syntax: collectionName.updateMany({filter},{Set Command}) Example:

14. Delete Document of a Collection

To delete the document, MongoDB consist of deleteOne() and deleteMany() methods. Syntax of delete methods are:

deleteOne({condition}) removes the single document meeting the deletion criteria.

 Syntax: collectionName.deleteOne({DeletionCondition}) Example:

deleteMany() removes all the documents matching the deletion criteria. Without the deletion criteria deleteMany({condition}) removes all the documents.

 Syntax: collectionName.deleteMany({DeletionCondition}) Example:

remove() There is another method to delete all the documents matching the deletion criteria. remove() method takes two arguments, one is deletion condition and the other is just one flag.

        Note: Remove method is deprecated in upcoming versions. Syntax: collectionName.remove({DeletionCondition},1) Example:

15. Retrieve Distinct

The distinct() method is used to get unique records.

To get distinct records from one field.

Syntax: collectionName.distinct(field) Example:

To get distinct records from one field while specifying the query.

Syntax: collectionName.distinct(field,query) Example:

16. Rename collection

MongoDB provides renameCollection () method to rename collection. Syntax: collectionName.renameCollection(newCollectionName) Example:

Indexing

17. Create Index on Document

Indexes are a special data structure that stores a small part of the collection’s data set in easy to traverse form. Indexes support ascending and descending ordering of fields values and hence facilitate better performance while retrieval. MongoDB provides the default_id index. Also, MongoDB supports the creation of user-defined Indexes. MongoDB indexes are defined at collections level and it provides supports at field or sub-field of a document. Syntax of create the index is :

Create an index on a single field.

Syntax: collectionName.createIndex({Key:1}) In this, the key indicates the field on which index is created and 1 signifies ascending order. To create an index in descending order -1 can be used. Example:

Create an index on multiple fields.

Syntax: collectionName.createIndex({Key1:1,key2:1…keyn:1}) Example:

 18. Show Index on Document

MongoDB provides getIndexes() method to list all the indexes created on a document. Syntax: collectionName.getIndexes() Example:

19. Remove Index from Document

dropIndex() method is used to drop the single index and dropIndexes() method is used to delete multiple indexes.

Remove Single Index

Syntax: collectionName.dropIndex({key}) Example:

Remove Multiple Indexes.

Syntax: collectionName.dropIndexes({key1,key2…,keyN}) Example:

20. Limit retrieval of documents

limit() method helps to limit the number of documents returned. The limit() method accepts numerical arguments. Syntax: collectionName.find().limit(number) Example:

21. Skip retrieval of documents

MongoDB supports skip() method. This method skips the required number of documents. It accepts a numeric argument. Syntax: collectionName.find().skip(number) Example:

22. Sort retrieval of documents

MongoDB sort() method sort the output documents either in ascending or descending order. This method accepts the name of keys with the number to specify sorting order 1 is used for ascending order whereas -1 is used to specify descending order. Syntax: collectionName.find().sort({key:1}) Example:

23. Document validation

Validators help to restrict the type of data being inserted in the documents. Validators are defined on collection. Validator creation is required to use keyword validator and optional validation level and validation action to specify the validation mode. Document validation doesn’t restrict the insertion of the new field in the document. Syntax: createCollection(“collectionName”,{validator:{ fields condition }}) Example:

24. Schema Validators on a new collection

Additional keyword $jsonSchema along with additional properties value as False is required to put restriction at the schema level. It prevents new fields to be added in the document. Syntax: createCollection(“collectionName”,{validator: { $jsonSchema { schema condition } }}) Example:

25. Update or Create Schema Validators on an existing collection

A validator can be created on existing collection using collMod Syntax: runCommand({collMod:”collectionName”,validator:{schema condition}}) Example:  

26. Remove Schema Validators on an existing collection

For removing schema validators it requires to set validationLevel as off. Syntax: runCommand({collMod:”collectionName”,validator:{ },validationLevel:off}) Example:

27. Check for Validators on an existing collection

To check if the existing collection is having schema validators run below command. Without specifying the collection name db.getCollectionInfos() method gives details of validators on all collections residing inside a DB. Syntax: getCollectionInfos({name : “collectionName”}) Example:

28. Cursor in MongoDB

The cursor is a pointer to iterate over the result set. MongoDB uses hasNext() and forEach() method for iteration. A list of cursor methods has been provided. Examples:

Utility

29. Taking a database backup

mongodump utility is used to export the content of the MongoDB database as a backup. This command runs from system console and not from mongo shell. It will generate binary backup along with metadata information. Syntax: mongodump –db dbName –out outFile –host “IP:PORT”  –username   –password  Example:

30. Restoring database from Backup

The utility mongorestore is used to restore binary data generated by mongodump. Syntax: mongorestore –db newDB “pathOfOldBackup” Example:

31. Exporting collections

To export content of collection to a file (JSON or CSV) mongoexport utility has been provided. To run this command use system terminal.

Export a single collection to a file.

Syntax: mongoexport –db dbName –collection collectionName –out outputFile Example:

Export a specific field from collection to a file.

Syntax: mongoexport –db dbName –collection collectionName –out outputFile –fields fieldname Example:

32. Importing collections

To import data from file (CSV or JSON) mongoimport command-line tool can be used. Syntax: mongoimport –db dbName –collection collectionName –file inputFile

33. MongoDB Replication

Replication is the process to synchronize data on multiple servers. It prevents data loss due to hardware or software malfunctioning.  MongoDB achieves replication using Replica sets. The replica set consists of Primary and secondary Mongo data sets in the cluster. Primary Data set accepts all write operations and secondary data set reads from Primary.  Minimum 3 data sets are required in Mongo Replica set.  Below process is required to set up a replica set:

Start mongod server with replset option on a minimum of 3 nodes.

mongod –port 27017 –dbpath C:\data\data1 –replSet rs0 –oplogSize 128 mongod –port 27018 –dbpath C:\data\data1 –replSet rs0 –oplogSize 128 mongod –port 27019 –dbpath C:\data\data1 –replSet rs0 –oplogSize 128

Initialize the replica set.

rs.initiate( {    _id : “rs0”,    members: [   { _id: 0, host: “IP:27017” },   { _id: 1, host: “IP:27018” },   { _id: 2, host: “IP:27019” }    ] })

34. Check the status of Replication

Run below command from the primary replica node to get complete info of the replica set. rs.conf() rs.status()

 35. Add new MongoDB instance to a replica set

Start Primary MongoDB client and run below command Syntax: rs.add(“hostname:port”) Example:

36. Remove existing MongoDB instance from the replica set

The below command will remove the required secondary host from the replica set. Syntax: rs.remove(“localhost:27017”) Example:

 37. Make Primary as Secondary replica set

MongoDB provides a command to instruct primary replica to become a secondary replica set. Syntax: rs.stepDown( stepDownSecs , secondaryCatchupSecs ) Example:

38. Check the Replica Lag between primary and Secondary

The below command will be used to check the replication lag between all replica set from the primary. Syntax: rs.printSlaveReplicationInfo() Example:

39. Transactions in MongoDB

MongoDB supports ACID properties for transactions on documents. To start a transaction, a session is required to start, and commit is required to save changes to the database.  Transactions are supported on replica set or mangos. Once the session committed successfully, operations made inside the session will be visible outside.

Start session

Syntax: session =db.getMongo().startSession()

Start transaction,

Syntax: session.startTransaction()

Commit transaction

Syntax: session.commitTransaction() Example: Let’s create a session, start the transaction, perform some insert/update and then commit the transaction.

40. Single Document Transactions Conflict

If two transactions tried to update the same document, MongoDB throws write conflict error.

session1.startTransaction() session2.startTransaction()

Perform some insert/update on Session1 followed by on Session2. Now observe the error in the below example Example:

41. Multi-Document Transactions

MongoDB support multi-document transactions in a single session.

db.getMongo().startTransaction()

Perform some insert/update on multiple documents

session.commitTransaction()

Example:

42. Profiling in MongoDB

Profiling helps in logging slow queries in the system.profile collection. Profiler level and sample rate define the percentage of queries to be logged in system.profile collection.

Set/Get Profiling level

Syntax: db.setProfilingLevel(profilingLevel,{“slowms”:time,“sampleRate”:LoggingPercentage})

Get Profiling Status

Syntax: db.getProfilingStatus()

To enable profiling at MongoDB instance level, start the instance with profiler information or add profiler details in the configuration file.

Syntax: mongod –profile –slowms

43. MongoDB Explain()

MongoDB explains() method returns statistics and provides information to select a winning plan and execute it to completion. It returns results as per the verbosity plan. Syntax: collectionName.explain(“verbosityName”) To execute explain() method/command, let’s create a verbosity and then execute explain() method, have a look at the below example, where these steps have been executed. Example:

44. Access Control in MongoDB

Access control features enable authentication access to existing users.  For access control enabled DB to ensure to create a user admin role in admin DB.

Connect DB without authentication. Switch to the database

use admin

Create user like below

db.createUser( {user: “UserAdmin”, pwd: “password” ,role: [adminRole])  Example:

Restart mongod instance Access again with created user and password.

45. Retrieve and Delete access control user

The below command can be used to check the user info and delete it.

db.getUser(“AdminUser”) db.dropUser(“AdminUser”)

Example:

46. Grant User-defined Roles

MongoDB provides db.createRole() method to specify the privileges to a user and an array of inherited roles.

Connect MongoDB instance with admin User. Execute below command to generate a new role.

Syntax: db.createRole({role:”roleName”,privileges:[{privilegeName}],roles:[InheritedArray]}) Example:

47. Revoke User-defined Roles

To modify the existing roles use below command. Syntax: db.revokeRolesFromUser( userName, [{ “role” : roleName , db:dbName} ] ) Example:

48. MongoDB Connectivity with Python

The pymongo package is required to connect MongoDB from python Console.

What’s next?

Check out this list of NoSQL clients to manage MongoDB and other NoSQL databases. If your job involves frequently working on MongoDB then you may want to learn more from this Udemy course.

48 MongoDB Commands and Queries to Know as Developer and DBA - 2048 MongoDB Commands and Queries to Know as Developer and DBA - 5348 MongoDB Commands and Queries to Know as Developer and DBA - 6948 MongoDB Commands and Queries to Know as Developer and DBA - 2948 MongoDB Commands and Queries to Know as Developer and DBA - 7848 MongoDB Commands and Queries to Know as Developer and DBA - 5648 MongoDB Commands and Queries to Know as Developer and DBA - 6748 MongoDB Commands and Queries to Know as Developer and DBA - 3948 MongoDB Commands and Queries to Know as Developer and DBA - 4348 MongoDB Commands and Queries to Know as Developer and DBA - 9848 MongoDB Commands and Queries to Know as Developer and DBA - 7948 MongoDB Commands and Queries to Know as Developer and DBA - 5948 MongoDB Commands and Queries to Know as Developer and DBA - 8448 MongoDB Commands and Queries to Know as Developer and DBA - 9248 MongoDB Commands and Queries to Know as Developer and DBA - 7548 MongoDB Commands and Queries to Know as Developer and DBA - 5348 MongoDB Commands and Queries to Know as Developer and DBA - 3748 MongoDB Commands and Queries to Know as Developer and DBA - 3548 MongoDB Commands and Queries to Know as Developer and DBA - 5048 MongoDB Commands and Queries to Know as Developer and DBA - 3148 MongoDB Commands and Queries to Know as Developer and DBA - 6148 MongoDB Commands and Queries to Know as Developer and DBA - 6848 MongoDB Commands and Queries to Know as Developer and DBA - 94