GETTING STARTED WITH MONGODB 
Tags: mongodb, tutorial, webdev, beginners
Published: 2021-09-13 14:23:01

## Introduction
In this article, I am going to introduce Mongodb,step-by-step basic implementation and explanation.

This article covers the following areas of MONGODB.

* Introduction of MongoDB
* Installation of MongoDB
* MongoDB terms vs SQL terms
* Brief overview of how MongoDB works
* Adding items to a database

## Introduction of MongoDB
[MongoDB](https://www.mongodb.com/) is an open-source [document-based database](https://www.mongodb.com/document-databases) management tool that stores data in JSON-like formats. It is a highly scalable, flexible, and distributed NoSQL database.

## Installation of MongoDB
You need to install MongoDB on your computer before you can connect to it. You can install MongoDB by following these instructions [Mac](https://www.prisma.io/dataguide/mongodb/setting-up-a-local-mongodb-database) and [Windows](https://www.prisma.io/dataguide/mongodb/setting-up-a-local-mongodb-database).

Try entering `mongod --version` into your command line once you've finished the installation procedure. You should receive anything along these lines:

```
mongod --version
```
![MongoDB Command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxac0a09g7ku4yyeivkd.PNG)

To Check MongoDB Shell version:
```
mongo --version
```
![MongoDB Shell](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dxyj3b8mk5p1iypunad8.PNG)

## STARTING MONGODB
You can start MongoDB in the command line with the `mongod` command.
By default, mongodb server will start at port `27017`
```
mongod
```
![Mongod Cmd](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ndzlacl36doafb35xvkz.PNG)

Keep the `mongod` window running when you want to work with your local MongoDB. MongoDB stops when you close the window

## MongoDB vs SQL Terminology
	
| Terminology      | Description    
| ------------- |:-------------:| 
| field    | A name-value pair. It is similar concept of column of an RDBMS | 
| document   | A group of `fields` are termed as document. In RDBMS we will term it as row. MongoDB document follows JSON syntax but it is a BSON syntax (BSON is an extended version of JSON implemented by MongoDB).  | 
| collection | 	A group of documents is called collection in MongoDB. A collection is similar concept of table of an RDBMS.   | 
| database | A Physical Container for Collections. Each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases  | 


My most recent client, a large stockbroking firm, uses MongoDB in addition to SQL Server. Recently, I had the opportunity to work with their MongoDB Expert, who is also in charge of one of their SQL Servers, which was underperforming.

The terminology we used for various database elements presented the biggest challenge for both of us when discussing the database. While I was referring to a record or row, he was referring to a document, and we were frequently lost in translation. To avoid any misunderstandings, I've created a small table that maps MongoDB terms to SQL terms.

| SQL Terms   | MongoDB Terms | 
| ------------- |:-------------:| 
| database   | database | 
| table  | collection | 
| row | document or BSON document   | 
| column | field  | 
| Index | Index | 
| table joins | embedded documents and linking | 
| primary key.Specify any unique column or column combination as primary key. | primary key.In MongoDB, the primary key is automatically set to the _id field. | 
| aggregation (e.g. group by) | aggregation pipeline | 

| SQL Concepts   | MongoDB Aggregation Operators | 
| ------------- |:-------------:| 
| WHERE  | $match | 
| GROUP BY  | $group | 
| HAVING | $match   | 
| SELECT | $project | 
| ORDER BY | $sort | 
| LIMIT | $limit | 
| SUM() | $sum | 
| COUNT() | $sum | 
| join | $lookup | 

## Brief overview of how MongoDB works
MongoDB lets you store things (called documents) inside databases. Each database contains multiple collections.

To make it easier to understand, you can think of MongoDB as a institutions. It contains many element.

Each element is a database. Each database is responsible for storing information about one application. You can store as much information as you want.

Let take the school for instance.Each department is a collection. Each collection can only contain one type of data.

For example, one collection can be used for instructors, one collection for users, one collection for courses, and so on.

## Adding items to a database
One way to add items to a MongoDB database is through the Mongo Shell. To open up the Mongo Shell, you open another command line window and run `mongo`.
```
mongo
```
![Mongo Command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/woojad00sfm1bm49egbr.PNG)

> Note: Make sure you keep the mongod window open! You won't be able to interact with the Mongo Shell if you close the mongod window.

First, we need a database to work with. You can see the currently selected database with the db command. (By default, you should on the test database).

```
 > db
```
> Note: The > in the code above signifies the Mongo Shell. You don't need to type >. It is not part of the command.

![DB test](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k65dn6t6pqbf64ypoguj.PNG)

For this article, we'll create a database called `tweety`. You can use the `use <database>` command to create and switch to a new database.
```
use tweety
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hxpmpqmshxmbvh92q2wm.PNG)

We're going to add a user into the tweety database. Here, we need to put the user into a collection. We'll use users as the name of the collection.

To add an item to a collection, you can pass a JavaScript object into db.collectionName.insertOne().
```
db.users.insertOne({ firstName:"Christopher",
                     lastName:"Glikpo"
});
```
![Test 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gir9oqcrxn65q7el512k.PNG)

Let's add one user into the database before we continue.
```
db.users.insertOne({ firstName:"Ben",
                     lastName:"Attoh"
});
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ot4vjfee3wu1n4tqlty.PNG)
You can see the users we've added by using the find command. db.collectionName.find().
```
db.characters.find();
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9dm63q6qbnapn9rexp1m.PNG)

If you've reached this point, thank you very much. I hope that this tutorial has been helpful for you and I'll see you all in the next.


If you like my work, please consider
[![Buy me a coffee](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jm11knj7d8zwcvo150q2.png)](https://www.buymeacoffee.com/cglikpo)
so that I can bring more projects, more articles for you


If you  want to learn more about Web Development don't forget to to [follow me on Youtube!](https://www.youtube.com/c/ChristopherGlikpo/featured)