How to Set Up Your Own NoSQL Database
Due to its simple queries and flexible data models, NoSQL is steadily gaining popularity in the software industry. Are you developing an app using NoSQL as your chosen database option, too? Then to test your app on your local server, you need to set up a NoSQL database on your PC.
Some popular NoSQL databases include MongoDB, Cassandra, CouchDB, CouchBase, Riak, and HBase. But how can you set one up? Here’s how.
What Is a NoSQL Database?
Also called non-SQL, NoSQL is a non-relational database design based on collections and documents rather than tables and rows as found in SQL.
In NoSQL, a collection is an aggregation of documents. These documents are individual JSON objects available as key-value pairs. Thus a collection in NoSQL may hold a particular group of information. For instance, it might be a collection of users’ data.
Nonetheless, documents in a collection are independent of each other and lack a definite structure. As a result, the data in each can be different in length and content.
This flexibility makes it easy to append extra data to some documents while leaving others out, all without running into errors or null value constraints during queries or data entry. In turn, regardless of individual document length or content type, you can still pick out specific information from each of them.
Requirements for Setting up a NoSQL Database
The requirements for setting up a NoSQL database sometimes depend on your chosen NoSQL technology. But setting one up is fairly easy.
CouchDB and MongoDB, for instance, only require a few installations, and you’re good to go.
We’ll base this tutorial on MongoDB—since it’s the most-used NoSQL database management system.
Ultimately, as with many NoSQL databases, you need to install MongoDB’s dedicated server to run your database. This facilitates the communication between your app and the database.
While it’s not required, you might also want to download MongoDB Compass, a more friendly tool for viewing and managing collections and documents in a dedicated GUI.
If you prefer to manage databases in a command-line interface instead, you can also set up MongoDB shell commands in your terminal.
Key Differences Between SQL and NoSQL
SQL and NoSQL have specific use-cases. But what are the key differences between them?
- As columns in SQL point to related cells (data points), each key in a document also points to specific information (value).
- While SQL links tables in an object-relational mapping (ORM) system, NoSQL uses object document mapping (ODM) to interrelate collections in a database.
- Data stored in NoSQL databases are readily-available as unstructured JSON objects. SQL stores information in more organized data files.
- Unlike SQL, which has rigid schemas, NoSQL schemas are more flexible. Additionally, NoSQL doesn’t require time-consuming joins. So SQL queries sometimes pale in comparison to NoSQL’s.
How to Set Up MongoDB Database on Windows
As mentioned earlier, you have to download and install a few files to get MongoDB running on your PC. But more importantly, you’ll need to install the MongoDB server.
To get started, follow these instructions:
Go to the MongoDB download page to download and install the community version of the MongoDB Server.
Note: Once you’re on the download page, expand the MongoDB Community Server tab.
On the sidebar to the right, click Download to get the compatible version for your OS.
Once the download is finished, open the installation file to start installing MongoDB Server on your PC:
Click Next and accept the license agreement. Then click Next.
In the next Window, copy the file path in the Data Directory field and paste it somewhere you can edit it.
Replace data in the copied path with bin.
For instance, if the path copied initially is:
C:\Program Files\MongoDB\Server\5.0\data\
Replace this with:
C:\Program Files\MongoDB\Server\5.0\bin\
Once done, go back into the installation process and click Next.
In the next step, select Complete to use the default installation.
Leave the Install MongoDB Compass box checked to install the GUI tool. Then click Next.
Select Install to install the MongoDB server on your PC.
Set Up MongoDB Shell Commands in Your Windows Terminal
For Windows OS, you need to add the path you copied earlier into the system variable. Doing this allows you to run MongoDB commands from the terminal.
To do this:
Open a folder on your PC. On the left bar, right-click This PC. Then select Properties. This takes you to the About menu.
Alternatively, you can click the Windows icon or hit the Windows key on your keyboard. Click the Settings icon. Select System. Scroll to the bottom and select the About option.
Look to the right and select Advanced system settings.
From the next Window, click Environment Variables.
Double-click Path under System variables.
Click New. Paste the edited path in the provided field. Then click OK in all the windows to finish adding MongoDB to your system variable.
Now open your terminal and type the following command:
mongo --version
If the above command outputs the installed MongoDB version, it means you’ve successfully installed the MongoDB server on your local machine.
Adding MongoDB’s bin path to your environmental variables lets you run MongoDB Commands in your terminal without installing the dedicated MongoDB shell.
You can also search for MongoDB Compass via the search bar on your PC to ensure that you installed it along with the server.
If you can’t find MongoDB Compass on your PC, you can install it separately by downloading the installation file from the MongoDB Compass download page.
Set Up MongoDB on Mac
You can install MongoDB on Mac OS using the Homebrew option. Before you start, ensure that you have the latest version of Homebrew on your Mac.
If you don’t have Homebrew installed, run this command in your Mac terminal to install the latest version:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
To update brew instead:
$ brew update
Now go ahead and install MongoDB Server by running the following command in your terminal:
$ brew install mongodb-community
To start the server:
$ brew services start mongodb-community
That’s it. You can now use MongoDB as a choice database when building apps on your PC. And if you install MongoDB Compass, you can view databases and collections there.
Get Your NoSQL Database Working
As we mentioned earlier, there’s no shortage of NoSQL databases out there. You might want to consider simplicity and community support before setting one up. There may be a few differences in their setup processes, though. But getting a NoSQL database to work on a local machine often follows a similar installation pattern. The steps outlined above will give you the basic knowledge you need to install any NoSQL database.