mongodb

MongoDB is an open-source, document-oriented management system that provides high-performance, highly available, easily scalable, and a cross-platform database system. This NoSQL database is used by several big data users to manage and handle database applications and other complex job processing applications. It is considered as the best and highly-used database management system. Unlike relational database systems where data is being stored in tables as rows and columns, MongoDB architecture involves documents and collections.

Here in this article, we will discuss a brief of MongoDB commands and the feature of this framework. So let us begin our discussion with MongoDB.

Features of MongoDB

MongoDB commands use the concept of master and slave replications that can prevent database downtime. The feature is considered as quite useful for various reasons.  MongoDB database can run on multiple servers, so at the time of any failure, the data can be availed from any server. In other words, data availability is quite high in MongoDB. MongoDB is schema-less so is more efficient Auto-sharding feature is the is also present in MongoDB, with the help of which MongoDB distributes data across multiple physical partitions that are also known as shards. It helps in automatic load-balancing.

Why Use MongoDB?

MongoDB commands are used to control data that is placed globally; they can ensure the fast performance of framework and compliance. MongoDB commands are used to provide a flexible data model. They can be used for almost every small or big task that may be either to update a single record or to build an application from scratch For scaled applications, there will be no downtime

Popular Commands of MongoDB

Now we are going to discuss some of the useful and popular MongoDB commands:

1). To Create Database

In MongoDb a database can be created by using the following syntax:

Use Database_Name: Here through this command, a new database with the given name will be created, and if it will not exist in the database then a new with this name will be created. You can also check the existence of this database name through the following command:

DB Database_Name: By default, MongoDB comes with a temporary database that is named as “test”. If you will not specify the name of your database, then it will be automatically stored in the “test” database.

2). Drop Database

To delete or drop the database, you can use the following command:

Database_Name.dropDatabase()

Here again, if you will not specify the name of the database then “test” database will be deleted.

3). Command to Create Collection

MongoDB command that is used to create the collection follows this syntax:

DB.createCollection(name,options)

Here, in this command, the name will be the name given by you to your collections. Moreover, the “Options” parameter is optional, but if you feel the need then you can provide it as well.

4). Drop Collection

You can not only create the collections but also can delete or drop them as well. For this firstly you should know the existing database collection names, then you can use the command that is used to drop that.

Step 1: Check for the database existence:

use mydb123

Switched to db mydb

show collections

mycol

mycollection

ABC

Step 2: Drop the collection

Db.mycollection.drop()

 

5). Insert Document

If you want to insert data into a collection, then you can use these commands that are known as Insert() and save(). The way to use this command is given below:

Db.mycol.insert({

_id: ObjectId(5d5fas6709h),

title: ‘MongoDB_Overview’,

description: ‘MongoDB is no sql database,’

)}

Here the name of the collection is ‘mycol.’ Here in such case and situation if the database will not present, then it will be first created, and then the values will get inserted in that database.

6). To Query a Document

If you want to search for any information from the collection then you can use find() method. The method will show the query execution result in an unstructured way. However, if you will use pretty() method along with find() method, then you can get the results in a structured way.

Some Intermediate MongoDB Commands

Some of the intermediate commands are also available in MongoDB that can perform certain operations directly on the database values. These intermediate functions are given below:

A). Limit()

In MongoDB, you can use Limit() command if you want to limit the number of records that will be used in MongoDB. This function also accepts the argument of number type. The argument will be the number of documents that you want to be displayed. The syntax is given below:

db.COLLECTION_NAME.find().limit(Number)

 

B). Sort()

You can also sort the records of MongoDB database by using following command. However, you can sort the data either in ascending of descending order by using 1 or -1 respectively. The syntax is given below:

db.COLECTION_NAME.find().sort({KEY:1})

C). How to use Indexing ?

In MongoDB, indexing function is used to make sure that the documents are scanned in efficient way to find any information. The syntax of this command is:

db.COLLECTION_NAME.ensureIndex{(KEY:1)}

D). Aggregate Command

If you want to get the calculated result by performing caclculation on data, then you can use Aggregate Command. This command can group the values from multiple documents. The syntax of the command is given below:

db.COLLECTION.aggregate(AGGREGATE_OPERATION)

Here the argument is AGGREGATE OPERATION as this operation can be any mathematical operation on the data values

E). How to Create and Restore Backup

If you want to create a database backup, then you can use mongodump command. By this command the whole data. The server data is usually get dumped be default into /bin/dump directory. The syntax of the command is:

mongodump – -host HOST_NAME – -port PORT_NUMBER

The backup can be restored through the following command:

mongorestore

Finally

You can find several MongoDB commands in the cheatsheet of this framework. By practicing these commands, you can maintain highly available, efficient, and scalable operations. Today every business is looking for such database, and MongoDB can help them in managing it efficiently.