MongoDB is a NoSQL database that stores the database in the form of JSON documents. We start by installing MongoDB on our PC. There are several variants based on development needs. These variants are as follows:
- MongoDB Atlas
- Community server
- MongoDB Enterprise Edition
All three are compatible with every operating system. You must first download the installation files according to your operating system. The installation methods for different operating systems are different. Here, we discuss the installation method in Windows and macOS.
Installation in Windows:
You must download an executable .msi package for installing MongoDB into Windows. Execute the following commands for installation:
> cd /setup-folder/
> msiexec.exe /q /i .msi ^
INSTALLLOCATION=”C:\Program Files\MongoDB\”
ADDLOCAL=”MonitoringTools,ImportExportTools,MiscellaneousTools”
These commands will take you to the specified directory. After installation, you need to configure the default database storage path for MongoDB using the following command:
> md \db\data
Installing in macOS:
Download the .tgz file which contains the necessary binaries. Unarchive the files, you can see a collection of binaries located in the bin folder. Move the bin folder to the required location. Open the directory and change the directory to the bin directory. Execute the following command to create a database in that location:
$ ./mongod --dbpath /path-to-desired-directory/
In this command, you must replace the directory path with the desired path and the server will be started when this command is executed.
Creating the first collection: The data is stored in the form of JSON documents which is collectively termed as collections. These collections are analogous to tables in RDBMS and the documents are analogous to records. In order to create a collection, we must first create a database and connect it to a command line. You need to execute the following command:
$ ./bin/mongo tutorial
This command will connect to the tutorial database and start the database simultaneously. You can notice some lines in the log to specify that the command line is connected to the database.
Execute the following command to create a collection:
$ > db.createCollection(‘Collection Name”);
This creates an empty collection. We will insert data into this collection and perform some operations in this data.