what is mongodb and why should we start learning it

What Is MongoDB And Why We Should Start Using It?

We all have heard and use MySQL or Relational Databases in our tech career. There is a long list of common relational databases, and the most popular relational DBs are Oracle, MySQL, SQL Server, and SQLite.

If you think to start learning MongoDB then in this blog post, I will try to explain What is MongoDB and What are the necessary commands to manage it?

What Is MongoDB?

MongoDB is a cross-platform most trusted NoSQL database, and it is written in C++, Javascript, and Python.

MongoDB uses collections and documents as MySQL uses tables and rows. We can quickly scale it horizontally by using Sharding and distributing data across several machines.

It is not compulsory to define schemas in Mongo. We can store data without any schema, and if you want to create schema before inserting data, you can also do that in Mongo.

Difference Between NoSQL And Relational Databases

Relational Database

  • MySQL is a relational database that is used as a tabular format, and we need different rows with the pre-defined schema to store data.

  • Relational Databases are vertically scalable, for example, if you want to scale your applications’ database, then you need to more hardware, investment, and workload on developers.

  • In relational databases, if you crave to store post in the table, then you also need a table for saving comments of that distinct post. And if you want to retrieve post and comments, you need to create a join between post and comments table.

Also Read: How to install xampp server

NoSQL Database

  • NoSQL databases are more likely to used column-based architecture. In NoSQL, you can name tables as collections and rows as columns or documents.

  • Most NoSQL databases storing data as JSON documents format and MongoDB database is one of the best examples of JSON based data.

  • In NoSQL databases, you no need to define or use any schema to store data into the database. You can also insert comments in post table(Collection in MongoDB).

24 Basic MongoDB Commands That Can Help You To Learn MongoDB.

I assume that you guys already installed MongoDB environment in your system. First of all, let’s open up the command line and navigate to your MongoDB installation directory.

Inside MongoDB directory let’s navigate to the server folder > current version folder > and then the final directory of the bin folder.

Okay, we are now inside our bin directory by the command line and let’s type your very first command mongod. If you do all the steps as it is, then you will see this message in the command line, waiting for connections on port 27017. Please leave this command line opened.

Open up another Command Line

Now open up another command line and navigate to the bin directory of our MongoDB installation folder, and then type this command mongo and wait. If everything is fine, then you will see this message in your very first command line connection accepted from 127.0.0.1:59175.

All the Commands Reference

To Show All Available Databases
Command->
show dbs

Minimize the first command line and now focus on the second command line and write show dbs to get all available databases list.

NOTE: We should always use the ID instead of title or other fields to update and delete any record into the database.

To use a Database
Command-> use dbName

To show collections of a particular database
Command-> show collections

To drop a database
Command-> db.dropDatabase()

To create a database
Command-> use dbName


Also Read: Best coding challenge websites

You need to create collections for the newly created database to show up in the DB listing.

To create collection
Command-> db.createCollection('collectionName')

To insert data into DB collection
Command-> 
db.post.insert 
({
      title: 'Post One',
      body: 'Body of the post',
      category: 'News',
      likes: 4,
      date: Date()
})

To insert multiple records
Command-> 
db.post.insertMany
([
    {
      	title: 'Post One',
	body: 'Body of the post',
	category: 'Technology',
	date: Date()
    },
    {	
        title: 'Post 2',
        body: 'Body of the post 2',
	category: 'News',
        date: Date()
    }
])


The post represents a collection in insert data and insertMany commands. If this collection is not available in the database then this command will create first a collection of post and then insert data into this.

To fetch data from the database
Command-> db.collectionName.find()

To fetch data in a pretty or readable format
Command-> db.collectionName.find().pretty()

To find specific data (Like where condition)
Command-> db.collectionName.find({fieldName: 'post 2'})

To fetch data sort by ASCE order
Command-> db.collectionName.find().sort({fieldName: 1})

To count documents
Command-> db.collectionName.find({category: 'News'}).count()

To apply limit condition
Command-> db.collectionName.find().limit(2)

To sort with a limit
Command-> db.collectionName.find().sort({title: -1}).limit(2)
In this command the -1 is use for DESC order.

To loop through data from the database
Command-> 
db.collectionName.find().forEach(function(doc){
      print('Blog Post:' + doc.title)
}) 

To find one specific record
Command-> db.collectionName.findOne({category: 'News'})

To update a record
Command-> 
db.collectionName.update({title: 'Post Two'},
 {
   title: 'Post Two',
   body: 'New Post 2 Body',
   date: Date()
 },
 {
   upsert: true
 }
)

Update method 2
Command-> 
db.collectionName.update({title: 'Post Two'}, 
  {
      $set: {
		body: 'Body of post 2',
		category: 'Technology'
	    }
  }
)


Increment Operator
Command-> 
db.collectionName.update({title: 'post one'},
	{
		$inc: { likes: 2 }
	}	
)

To Rename a field name by using rename operator
Command-> 
db.collectionName.update({title: 'post name'}, 
	{
		$rename: { likes: 'views' }	
	}
)

To delete a record from MongoDB collection.
Command-> db.collectionName.remove({title: 'post four'})

To filter data using greater, less than and equal to

Greater than-> db.collectionName.find({views: {$gt:2}})
Greater than or equal to-> db.collectionName.find({views: {$gte:2}})
Less than-> db.collectionName.find({views: {$lt:2}})
Less than or equal to-> db.collectionName.find({views: {$lte:2}})


Conclusion

So far so good in this blog post I did explaining that what is MongoDB and what are the basic commands of MongoDB.

I hope you enjoyed this article.

Please leave a comment if I skip any relevant topic or command of MongoDB. If you like this post, then please do share it with your friends and on social media platforms.

Rakesh Kumar

About the Author: Rakesh Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *