DEV Community

Bhagya
Bhagya

Posted on • Edited on

Getting Started With Non-relational Databases Using Mongodb 🍃

What is a relational database?

If you’ve worked with databases for a while, chances are, you started your career using a relational database. Examples of a relational databases are Microsoft Access, MySql, Oracle, etc.

A relational database is a conventional database that uses tables to store data. In a relational database, each field data type is defined. That is if you define a particular field to accept only numbers for example age of a person, that field will not accept any character from a-z.

What is a non-relational database?

Before discussing fully what a non-relational database is, it is needful to define some key terminologies which can help us better understand a non-relational database and how it operates. These are:

  • Key-value pairs.
  • Document.
  • Collections.

Key-value pairs

Key-value pairs are identifiers and corresponding values. A simple way to know how this work is assuming one walks into a bar and asks for a waiter. The “waiter” in this scenario is the identifier while the name of the waiter for example “Bhagya”, is the value. So in JSON format, this can be represented as shown below.

{
    "waiter": "Bhagya"
}
Enter fullscreen mode Exit fullscreen mode

What is a document?

Documents are a set of JSON data. It is basically a set of key-value pairs that give you detailed information about an entity. Below, is an example of a document for our waiter example. It contains personal information about the waiter.

{
    "id": 1,
    "name": "Bhagya",
    "username": "bhagya",
    "email": "bhagya@mongo.com",
    "phone": "+947623496905",
    "website": "bhagya.org"
}
Enter fullscreen mode Exit fullscreen mode

What is a collection?

Collections are a set of documents. That is information about more than one entity. In a relational database, collections are what are known as tables.

[
  {
    "id": 1,
    "name": "Bhagya",
    "username": "bhagya",
    "email": "bhagya@mongo.com",
    "phone": "+947623496905",
    "website": "bhagya.org",
  },
  {
    "id": 2,
    "name": "Nimesh",
    "username": "Nimezzz",
    "email": "Snimesh@codebrix.com",
    "phone": "+94710876322",
    "website": "nimesh.net",
  },
  {
    "id": 3,
    "name": "Nethmini",
    "username": "nethmini",
    "email": "nethmini@bk.net",
    "phone": "+9476245555533",
    "website": "nethmini.info",
  }
]

Enter fullscreen mode Exit fullscreen mode

So with the aforementioned defined terms, we can define a non-relational database as a database that stores data in JSON-like format which is achieved through key-value pairs. Examples of non-relational databases are MongoDB, Redis, Couchbase, etc.

Getting started with MongoDB?

MongoDB is a non-relational database. That is, it doesn’t store data in columns and rows but in BSON format. It is an open-source database founded in 2007. Open-source applications are software whose codebase is open to contributions from other developers.

Working with MongoDB can be in two variants. We can either access it online using MongoDB atlas or download it locally on our computer. For local downloads, we have two editions which are enterprise and community editions. The enterprise edition is the paid version while the community version can be used for free, obviously with limited capabilities when compared to the former.

We can access it online via

• MongoDB shell

• MongoDB compass

Registering for MongoDB Atlas

Before using any of the tools to start performing database operations, you need to register an account with MongoDB Atlas. It is a cloud database, that is all instead of hosting your own server, you just need to register and it’ll take care of what you need to do at the backend. To create an atlas account, follow the steps below

  • Go to mongodb.com
  • For practice, click on the Try Free option. This allows you to work with Atlas for development purposes.
  • A bio-data form will come up, fill in the required details. You can also use your Gmail account to sign up.
  • Next, you log in using the login details you just created
  • You can now proceed with deploying a free cluster by clicking on Build a cluster.
  • Three different option is available to you, the dedicated and serverless option are both paid version, for development purpose, click on the shared version.
  • You can now proceed to select a cloud provider and region.
  • Click on create cluster button.
  • After registration, you need to create a username and password alongside enabling database access.
  • Click database access. It will prompt you to add a database user.
  • Enter your preferred username and password.
  • Click on network access on the left pane.
  • Click on Add IP Address.
  • You can choose to allow specific IP addresses, or for development purposes, you can just allow access from anywhere.
  • Click on confirm. Now you can proceed to use MongoDB Shell or Compass.

Using MongoDB shell

MongoDB shell is a command-line environment that can be used to access your database.

Using MongoDB compass

MongoDB Compass is a tool like MongoDB shell that can be used to interact with our database. However, unlike the MongoDB shell which is primarily a command prompt interface, compass has a graphical user interface that makes it easy to work with.

To download and work with compass, follow the instruction below.

  • Download and install.
  • Go back to your Atlas homepage, and click on connect.
  • Click on connect using MongoDB Compass.
  • Copy the connection string from Atlas and paste it into the file path shown below.
  • Click connect.

If your connection is successful, you should see all databases created will be listed. You can view them by clicking on each one. And you can also create a new database by clicking on the create database button.

MongoDB compass lets you create your database, collection, and document in a very seamless way. If you would like to explore more, you can refer to the official documentation. For this tutorial, we’ll be focusing on the MongoDB shell which is a little bit complex. So let’s look at how we can perform CRUD operations using the MongoDB shell.

Performing CRUD operations on our database

CRUD is an acronym for creating, reading, updating, and deleting in programming. For one to perform the read, update or delete operation, a record has to be created initially on the database. Creating a record is adding data about an entity. Read operation means to get or view records in the database. The update operation is to make changes to the record that is in view. Delete operation means to remove a specific record.

Top comments (0)