Starting with Mongodb

Installation and configuation

Download following mongodb packages from https://repo.mongodb.org/yum/redhat/


mongodb-org-mongos
mongodb-org-server
mongodb-org-shell
mongodb-org-tools
mongodb-org-database-tools-extra
mongodb-database-tools

Install packages:


rpm -Uvh mongodb-org-mongos-4.4.4-1.el7.x86_64.rpm mongodb-org-server-4.4.4-1.el7.x86_64.rpm mongodb-org-shell-4.4.4-1.el7.x86_64.rpm mongodb-org-tools-4.4.4-1.el7.x86_64.rpm mongodb-org-database-tools-extra-4.4.4-1.el7.x86_64.rpm mongodb-database-tools-100.3.0.x86_64.rpm

Edit /etc/mongod.conf and amend systemLog->path to desired log location and storage->dbPath to desired database location. For eg, if storage->dbPath is set to /mongodb/data, then also change owner of this directory as following:

chown -R mongod:mongod /mongodb/data

Start mongodb service and tail logs in parallel

systemctl start mongod

View logs:


tail -f /var/log/mongodb/mongod.log

{"t":{"$date":"2021-03-02T10:54:07.549+05:30"},"s":"I",  "c":"CONTROL",  "id":20698,   "ctx":"main","msg":"***** SERVER RESTARTED *****"}
{"t":{"$date":"2021-03-02T10:54:07.561+05:30"},"s":"I",  "c":"CONTROL",  "id":23285,   "ctx":"main","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}
{"t":{"$date":"2021-03-02T10:54:12.595+05:30"},"s":"W",  "c":"ASIO",     "id":22601,   "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"}
{"t":{"$date":"2021-03-02T10:54:12.597+05:30"},"s":"I",  "c":"NETWORK",  "id":4648601, "ctx":"main","msg":"Implicit TCP FastOpen unavailable. If TCP FastOpen is required, set tcpFastOpenServer, tcpFastOpenClient, and tcpFastOpenQueueSize."}
....
....

Once package starts clean, then you can enable service at startup.

systemctl enable mongod

Create database and add a few records


> use Galaxy
switched to db Galaxy

> db
Galaxy
> db.Galaxy.insert({"Object":"Planet", "Name": "Saturn"})
WriteResult({ "nInserted" : 1 })

> db.Galaxy.insert({"Object":"Star", "Name": "Sun", "Age": "4.603b"})
WriteResult({ "nInserted" : 1 })

> db.Galaxy.insert({"Object":"BlackHole", "Name": "XXXX", "Size": "10"})
WriteResult({ "nInserted" : 1 })

Fetch added records


> db.Galaxy.find({})
{ "_id" : ObjectId("603e02590fd2fbf10814926c"), "Object" : "Planet", "Name" : "Saturn" }
{ "_id" : ObjectId("603e04070fd2fbf108149270"), "Object" : "Star", "Name" : "Sun", "Age" : "4.603b" }
{ "_id" : ObjectId("603e043f0fd2fbf108149271"), "Object" : "BlackHole", "Name" : "XXXX", "Size" : "10" }

> db.Galaxy.find({"Name": "XXXX"})
{ "_id" : ObjectId("603e043f0fd2fbf108149271"), "Object" : "BlackHole", "Name" : "XXXX", "Size" : "10" }

Delete record


> db.Galaxy.remove({"Object" : "BlackHole"})
WriteResult({ "nRemoved" : 1 })

> db.Galaxy.find({})
{ "_id" : ObjectId("603e02590fd2fbf10814926c"), "Object" : "Planet", "Name" : "Saturn" }
{ "_id" : ObjectId("603e04070fd2fbf108149270"), "Object" : "Star", "Name" : "Sun", "Age" : "4.603b" }

Update records


> db.Galaxy.find({})
{ "_id" : ObjectId("603e02590fd2fbf10814926c"), "Object" : "Planet", "Name" : "Saturn" }
{ "_id" : ObjectId("603e04070fd2fbf108149270"), "Object" : "Star", "Name" : "Sun", "Age" : "4.603b" }

> db.Galaxy.update({"_id": ObjectId("603e02590fd2fbf10814926c")}, {"Name" : "Saturn", "Size": "20"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.Galaxy.find({})
{ "_id" : ObjectId("603e02590fd2fbf10814926c"), "Name" : "Saturn", "Size" : "20" }
{ "_id" : ObjectId("603e04070fd2fbf108149270"), "Object" : "Star", "Name" : "Sun", "Age" : "4.603b" }