+91 9619904949

There are more than 379 database servers in use around the world today. Among them, MongoDB stands out as a top performer, surpassing databases like HBase, Neo4j, Riak, Memcached, RavenDB, CouchDB, and Redis. Tech giants like Google, Yahoo, and Facebook rely on MongoDB in their production environments.In the DB-Engines ranking, MongoDB holds the 5th position overall, following:

Oracle
MySQL
Microsoft SQL Server
PostgreSQL
MongoDB

Notably, MongoDB is also ranked as the number one NoSQL database.

RDBMS Mongo
Database database
table Collection
Record Document
Joins Embedded Object/Document

Mongo works perfectly with most all programming languages.
Also, mongo works with Windows and Linux with the same performance and without issues.
Mongo provides Replication, Sharding, Aggregation, and indexing features.
Mongo is an object-oriented and schema-less database.

Mongo is based on JavaScript, and all documents (Records) are presented in JSON format. Also, in Mongo, everything is an object. In Mongo 1st field, the compulsory field is _id which is not skippable.

{
_id: numeric or numeric-alphabetical string or,it set automatically. Mongo id is a 12-byte Object id that is a 4-byte time-stamp with 5 bytes of any random value and 3 bytes of a counter value.
}

mongo document stores like
the document content single field, array, sub-document (join), or, an array of sub-document.
{
_id: 1
First name: Anil,
middle name : Jasvantray,
last name: Jalela
Mobile: [9619904949,2573335,2567580]
}

install mongo:-

1 add Repo echo “deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse” > /etc/apt/sources.list.d/mongodb-org-6.0.list
2 add Key wget -qO – https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add –
3 update package list apt-get update
4 install mongo server apt-get install -y mongodb-org && apt-get install mongodb-org-server
5 change dir for modification cd /etc/
6 rename conf mv mongod.conf mongod.conf_org
7 vi mongod.conf  and add the below content into this.
8 # mongod.conf

# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
# engine:
# wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0

# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

security:
  keyFile: /etc/keyfile-mongo
authorization: enabled

#operationProfiling:

#replication:
#  replSetName: “election01”

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

9 kerate key file on master for replica set openssl rand -base64 756 > /etc/keyfile-mongo
10 change permission chmod 600 /etc/keyfile-mongo && ll /etc/keyfile-mongo
11 change onership of files chown mongodb:mongodb /etc/mongod.conf /etc/keyfile-mongo
12 start  mongo sudo systemctl start mongod
13 start mongo on system boot sudo systemctl enable mongod
14 check mongo status sudo systemctl status mongod
15 mongo login command mongosh
16 use mongo database use admin
17 set mongo password db.createUser(
{
user: “mongoadmin”,
pwd: passwordPrompt(),
roles: [ { role: “root”, db: “admin” }, “readWriteAnyDatabase” ]
}
)
18   mongosh –username=mongoadmin –password=yourpass –authenticationDatabase admin
19 create database  use nitwings
20 drop database “use nitwings” and then “db.dropDatabase()”
21 create database-specific user db.createUser({
user: “blackpost”,
pwd: passwordPrompt(),
roles: [
{ role: “readWrite”, db: “nitwings” }
],
mechanisms: [“SCRAM-SHA-256”],
authenticationRestrictions: [
{
clientSource: [“0.0.0.0/0”]
}
]
})
22 drop user “use admin”  and then db.dropUser(“blackpost”);
23 show users
db.getUsers()
24 create collection use nitwings
db.createCollection(“testCollection”) 
25
Insert One Document into collection
db.testCollection.insertOne({ name: “test”, value: 123 })
26 Insert many Document into collection
db.testCollection.insertMany([{ name: “blackpost”, value: 789 }, { name: “nitwings”, value: 456 }])
27
create Index
db.testCollection.createIndex({ name: 1 });
28 check index created or not for collection
db.testCollection.getIndexes()
29 find document
nitwings> db.testCollection.findOne({ name: “blackpost” })
{
_id: ObjectId(’66d485a63c3f4eb31f5e739c’),
name: ‘blackpost’,
value: 789
}
30 update document
nitwings> db.testCollection.updateOne({ name: “blackpost” }, { $set: { name: “aniljalela” } })
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
nitwings> db.testCollection.findOne({ name: “aniljalela” })
{
_id: ObjectId(’66d485a63c3f4eb31f5e739c’),
name: ‘aniljalela’,
value: 789
}
nitwings>
31 delete document
nitwings> db.testCollection.deleteOne({  name: “aniljalela” })
{ acknowledged: true, deletedCount: 1 }
nitwings>
  show collections and drop collection
nitwings> show collections

nitwings> db..drop()

32 backup database mongodump –db nitwings –out /opt/backup/ –username mongoadmin –password yourpass –authenticationDatabase admin
33 backup database from remote mongodump –host 10.10.10.10 –port 27017 –db nitwings –out /opt/backup/ –username mongoadmin –password yourpass  –authenticationDatabase admin
34 mongo all database mongodump –out /backups/all_databases_backup –username mongoadmin –password yourpass –authenticationDatabase admin

 

35 mongo all database from remote  mongodump –host 10.10.10.10 –port 27017 –out /path/to/backup /opt/backup/ –username mongoadmin –password yourpass –authenticationDatabase admin
36 restore database dump
mongorestore –host 10.10.10.10 –port 27017 –db nitwings –username mongoadmin –password yourpass –authenticationDatabase admin /opt/backup/nitwings
37  drop existing DB and restore db
mongorestore –host 10.10.10.10 –port 27017 –db nitwings –username mongoadmin –password yourpass –authenticationDatabase admin –drop /opt/backup/nitwings
38  restore all databases
mongorestore –host 10.10.10.10 –port 27017 –username mongoadmin –password yourpass –authenticationDatabase admin /opt/backup/
39
drop and restore all databases
mongorestore –host 10.10.10.10 –port 27017 –username mongoadmin –password yourpass –authenticationDatabase admin –drop /opt/backup/
40  restore specific collection
mongorestore –host 10.10.10.10 –port 27017 –db nitwings –collection –username mongoadmin –password yourpass –authenticationDatabase admin /opt/backup/nitwings/.bson

 

If the –drop option is not used with mongorestore, MongoDB restores data without dropping existing collections. If a collection already exists, mongorestore merges the backup with the current data. Documents with the same _id are not overwritten; instead, they are skipped to avoid duplicates. New collections from the dump are created if they don’t exist. This approach can lead to inconsistent or duplicated data, especially if the data structure has changed since the backup was created, potentially causing incorrect query results. Indexes are restored as in the dump, but existing indexes are not recreated, and mismatched index specifications may cause the restore to fail.

Replica set:-
1.1.1.1 production-mongodb-01 master-node1
2.2.2.2 production-mongodb-02 slave-node1
3.3.3.3 production-mongodb-03 slave-node2

(1) Install Mongo on all  servers using the above steps from 1 to 17
(2) un-comment below lines from conf

replication:
  replSetName: “election01”

(3) keyFile: This is used for internal authentication between MongoDB instances in a replica set or sharded cluster.
It ensures that only authorized MongoDB instances can communicate with each other.

scp  /etc/keyfile-mongo  2.2.2.2: /etc/keyfile-mongo
scp  /etc/keyfile-mongo  3.3.3.3: /etc/keyfile-mongo

(4) restart  master and slave and log in to Mongo to start replication


1 login master mongosh –username=mongoadmin –password=yourpass –authenticationDatabase admin
2 Initiate replication rs.initiate()
3 add replica rs.add(“2.2.2.2”)
rs.add(“3.3.3.3”)
4 check replication status rs.status()
5 remove the replica from the replication rs.remove(“hostname:port”)
6 rs.reconfig({})
7 db.serverStatus()
8 db.currentOp()
9 db.repairDatabase()
10 db.stats()
db.collection.stats()