In this blog..lets try to understand …
What is daemon?
How to create a daemon server?
How to connect to that daemon server?…..

All databases, collections( or tables), documents(or records) are stored in the hard disk. Adding/deleting/reading records .. all these operations will be done on the hard disk itself.

Lets see the problem from this…

Problem Statement:

How to access those records from the disk in our code (node.js, python etc..)? ??
How to query in our code on the tables which is present on the disk?
Should we need to write the logic for find, update, delete queries by ourself?

Solution:

Mongo Daemon (or MongoD).

How the solution works:

  • mongoD (or mongo daemon) is an instance that will run on
    port 27017 (as default ) which points to the hard disk memory where the databases, collections( or tables), documents(or records) are stored.
  • Any db query from the code….this request is send to the mongo daemon…it then executes the query on the tables which is stored in the disk …finally sends the result back to the code.

Recap:
Any mongo query from code -> goes to mongoD (on port 27017 by default)
-> it executes the query on data which is on hard disk -> returns the result to the code.

PS: mongoD points to /var/lib/mongodb path by default where the database will be stored. We can change the mongoD to point to different directory and reads/writes will happen in that directory.

Commands:

mongod

when you type the above command in a terminal…it creates a mongo daemon instance on port 27017 (by default) and it will be pointing to the directory /var/lib/mongodb (by default) where all the databases are stored.

mongod --dbpath /home/db

Now you want to change the dbpath to different directory…then:
To change the path: use — dbpath
Type the above command in a terminal…it creates a mongo daemon instance on port 27017 (by default) and it will be pointing to the directory /home/db.
PS: Make sure the created directory should have proper permissions.

mongod --dbpath /home/db --port 27019

To change port: use — port.
Type the above command in a terminal…it creates a mongo daemon instance on port 27019 and it will be pointing to the directory /home/db.

Setting up Mongo daemon in local:

mkdir /home/db
chmod 777 /home/db
(new terminal(A)): mongod --dbpath /home/db --port 27019
(new terminal(B)): mongo --port 27019

1st command creates a directory at /home/db.
2nd command creates read/write/execute permission to owner, group and others to that directory.
3rd command creates a new daemon instance which will run on port 27019.

options: { net: { port: 27019 }, storage: { dbPath: “/home/db” }
this line in the logs ..tells the daemon will be started on port 27019 and the dbpath will be set to “/home/db”.

  • waiting for connections on port 27019
    This line tells it is waiting to get connected..usually we connect to this daemon in the code…or we can manually connect to the daemon using shell.

4th command must be typed in new terminal B : It creates new connection to the daemon server running on port 27019. If the --port is not provided..it will try to connect to 27017 port by default.

Once the connection is successful (in terminal A).. a new log will appear …it says new connection has been established.

Now once the connection has been established ..you will be able to see the log in the terminal A.

MERN stack developer