Menu

Introduction to MongoDb

What is MongoDb

MongoDb is NOSQL database.
MongoDb is a document database that stores data in json like documents with no particular format.
MongoDb is free and open source.

Advantages Of MongoDb

- Schema Less database.
- MongoDb is easy to scale.
- Replication and high availability.
- Auto-sharding.
- No complex joins are needed due to structuring way of data in mongodb.

Limitations Of MongoDb

- If you index some field, that field can’t contain more than 1024 bytes
- aggregate/$sort produces error if sorting takes more than 10 percent of RAM.
- A replica set can have up to 50 members, but only 7 voting members.

NOTE :- A replica set in MongoDB is a group of mongod processes that maintain the same data set. Replica sets provide redundancy and high availability, and are the basis for all production deployments.

- A collection cannot have more than 64 indexes.
- The length of the index name cannot be longer than 125 characters.
- A compound index can have maximum 31 fields indexed..

MongoDb Installation

Add mongodb repository.

		
sudo vim /etc/yum.repos.d/mongodb-org-4.0.repo

Check on mongodb official site for latest stable release.

		
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
	

save and exit.

verify that the MongoDB repository exists within the yum utility.

		
yum repolist

Install MongoDB

		
sudo yum install mongodb-org

start the MongoDB service with the systemctl utility:

		
sudo systemctl start mongod

Check Log :

		
sudo tail /var/log/mongodb/mongod.log
- An output of waiting for a connection confirms that MongoDB has started successfully and we can access the database server with the MongoDB Shell:

To stop mongodb server:

		
sudo systemctl stop mongod

MongoDb Basics

Mongodb basic commands for begineers :-

Show All databases
		
show dbs 
Show all tables /collections
		
show collections 	
Selecting database /creating database
		
use db_name
Create Collection
		
db.createCollection("myfirstcollection")
Inserting a document in collection
		
db.myfirstcollection.insert([{employee_id: 12345 ,employee_name: "mysqlgyan",employee_dept :"database"}])
Inserting multiple document in collection at once
		
db.myfirstcollection.insertMany([{employee_id: 67895 ,employee_name: "testing",employee_dept :"database"},{employee_id: 77895 ,employee_name: "testing_data",employee_dept :"database"}])
Updating document
		
db.myfirstcollection.update({employee_id:12345} ,{$set : {employee_dept :"alldatabase"}})
Updating multiple matching document
		
db.myfirstcollection.updateMany({employee_id:12345} ,{$set : {employee_dept :"alldatabaseupdate"}})
Display records / select query
		
db.myfirstcollection.find()	
Display records / select query - with limit
		
db.myfirstcollection.find().limit(2)	
Display records / select query - with where clause
		
db.myfirstcollection.find({employee_id:12345})
Display records / select query - with where clause and with specific colummns in result set
		
db.myfirstcollection.find({employee_id:12345},{employee_name:1})

NOTE :-by default id will come .

To remove id also
		
db.myfirstcollection.find({employee_id:12345},{employee_name:1 ,_id:0})
To count total number of records in collecton
		
db.myfirstcollection.count()

Support us by sharing our content :-

LinkedIn

Support us on Patreon :-
Become Patron