MongoDB is an open-source document database and a leading NoSQL database. MongoDB is a cross-platform, document-oriented database that provides high performance, high availability, and easy scalability.
MongoDB works on the concept of collection and documentation. It is not a Relational Database like MySQL or Oracle. In MySQL database, there is a Database, then Tables, and then Columns.
MongoDB structure is like a Database, then Collections, and then Documents. So there is no concept like Tables and Columns in MongoDB. Nevertheless, the MongoDB database is vastly popular with the Node.js platform.
NoSQL MongoDB
First, we learn the basic structure of the MongoDB database, like what is collections and what is a document. Then we will install MongoDB on Mac System and then play with it.
Collections
A collection is a group of MongoDB documents. It is the equivalent of a Relational Database table. A collection exists within a single database.
Document
The document is a set of key-value pairs. Documents have a dynamic schema. Dynamic schema means that documents in the same collection do not need the same fields or structure, and standard fields in a collection’s documents may hold different data types.
Example of Document
{ "_id" : ObjectId("5ac0ac19d412d208b8c91214"), "course_name" : "react.js", "course_price" : 10, "__v" : 0 }
It is like a JSON Object. The document contains key-value pairs. Here, _id is generated by MongoDB. It is equivalent to id in MySQL database; the values are different because, in most cases in MySQL, id is an incremental field starting from 1.
Install MongoDB on Mac.
If you have installed the MongoDB database on Mac, skip this step. For newbies, go to your terminal and hit the following command to install Homebrew. If you have installed it, then do not type this command.
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Next, add Homebrew’s location to your $PATH in your .bash_profile or .zshrc file.
export PATH="/usr/local/bin:$PATH"
Now, install MongoDB using Homebrew.
Type the following command in your terminal.
brew install mongodb
After downloading Mongo, create the “db” directory. This is where the Mongo data files will live. You can make the directory in the default location by running mkdir -p /data/db
Make sure that the /data/db directory has the right permissions by running the following command.
sudo chown -R `id -un` /data/db
Run the Mongo daemon in one of your terminal windows run mongod. It should start the Mongo server at port number 27017.
mongod
Now, your MongoDB server is ready to connect any web application. Once you are connected, you can see that one connection has been added.
While working on Any web project that uses the MongoDB database, you need to open this connection and never close the terminal.
Otherwise, the connection between a web application and a database will be lost. So never down the mongo server.
MongoShell Interface
Mongo is an interactive JavaScript shell interface to MongoDB, which provides a powerful interface for systems administrators and developers to test queries and operations directly with the database.
Now, we can use the following command to see how many databases are there by default.
show dbs
Create Database.
Now, we can create one database using the following command.
use demodb
If the database is not there, then it will create for us. If already there, then it switches to that database.
To check your currently selected database, use the following command.
db
Create Collection.
The cool thing about MongoDB is that you need not create a collection before inserting the document. Instead, add a document to the collection with a single command, and MongoDB creates that collection on the fly.
db.person.insert({name: 'mahavir', age: 70})
It will create a collection called the person and add one document.
So, you can see the inserted documents using the following command.
db.person.find()
For a prettier result, we can query like this.
db.person.find().pretty()
Shell Help
To see the list of help in the mongo shell, type the following.
help
It will list the commands. You can use it to find help in database administration, query the results, etc.
Now, let us say we need help to find all the documents in the person collection. Then we can go through help to execute the right query.
From starting, first, check whether you are in the correct database or not. So first, switch to the correct database.
use demodb
Then, check whether you are in the collection or not.
db
If not, then switch to the proper collection using the following command.
use person
Now, type the following code to get all the methods applicable to any collection.
db.person.help()
Now, you can apply the first command to return all the documents. But you can go further for help. Type the following command.
db.person.find().help()
So, you will see what other methods you can chain with the find() method to get the desired result.
You can also drop the database using the following command.
db.dropDatabase()
We will dive deep into the NoSQL MongoDB database in future posts. That is it for now; NoSQL MongoDB Tutorial is over.
Thanks for taking it.
Thank you, this website is great. I am learning how to connect React with Backend and your last tutorial about it was very powerful.
Great Tutorial!