UNDERSTANDING MongoDB SYNTAX
Tags: webdev, javascript, react
Published: 2022-11-13 11:56:56

   
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p3svxjaiq1z8db0vm183.png)

**Introduction**


## Let's start by defining what MongoDB

MongoDB is a document database with the scalability and flexibility you want with the querying and indexing you need. it's a NoSQL database(document database) data is stored as a document using JSON. 


## MongoDB offers many advantages over traditional relational databases:

- Full cloud-based developer data platform

- Flexible document schema

- Widely supported and code-native data access

- Change-friendly design

- Powerful querying and analytics

- Easy horizontal scale-out with sharding

- Simple installation

- Cost-effective

- Full technical support and documentation


**Installing MongoDB**


- 
First, you need to heed to [MongoDB](https://www.mongodb.com/) and download the MSI package of MongoDB.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5r7neju8mgzaul1dqd6n.png)

- 
Go ahead and run that MSI file

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uwgr5qge904vpod9tx9f.png)

- 
Choose custom to change the location for simplicity.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dfdwazvdcfctlb4uovo7.png)

- 
click on browse to create a folder in C drive, and click on this icon to choose C drive, then click on the icon next to that to create the folder called MongoDB.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z2ublo19zoqqannfwr8e.png)

- 
click next to finish the installation. once you finish installation open up the command as administrator. 

- 
Go to the MongoDB folder in C drive and create a folder called data and another one called log. Open the data folder and create another one called db where all data will be stored.

- 
Now let's go to the command line and navigate to the mongodb folder and then into the bin folder




![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ohyhhhc1utpnk996337u.png)

- 
Run the following command to specify the path to the folder we just created and create a file to where all MongoDB logs write to.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2n0fsjntof4iuao56xoj.png)

**Running MongoDB**

- 
Now we should be able to run the service with the below command


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cmwmiiiihs3oqsvf0wav.png)


- 
We are going to be working from Mongo shell and we can do that by typing the command **mongo** in the bin directory.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1vbx3suanknukzrobqic.png)

- 
We are now on the shell, you can type cls to clear everything out.

**MongoDB syntax**
- 
Let's now create a document database by running the following command, **use mycustomers**  (mycustomers) is the database name.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zty0o7yf0xhzbev1e9gu.png)


- 
 create a user of the database.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/55ipvxc8fogqgj753ixx.png) we can see the user has been created successfully.

- 
So now we have the user let's start to add some data, we first create collections that are just similar to tables in a relational database. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j65ragn1cnh9kdgijloy.png)  customers is the name of the collection.

- 
Insert data into the collection by running the following command. you can also run command **db. customers. find();** to confirm the document has been inserted.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5yvzii3k3o7wjf7usyi9.png)

- 
Take note that in **NoSQL databases** you do not have to specify any field you are adding, for example, we have an added gender field for Mark and others don't have. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/luqep0s7f68uwc1kksfx.png)

- 
 make the database a bit neat by running command , **db.customers.find().pretty();**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxjldur0tkn4f0fydgh2.png)  

- 
**updating fields**, let's say that to Dan Doe we want to add a gender field as well. The first parameter is to match the database we want to update. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ajwy3qh5fufsbqpc423r.png)

- We can also use the **set** operator to update, so what this operator does only adds whatever you want to add and keep the rest of the previous data.

- so let's say we what to add gender Steve, we run the following command.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5l2bimtyfa1emxbu6zoh.png)

- So if we run the command and then do a search we see **Steve Ian** now has male gender.

- We also have an operator called unset if we want to remove a field. so let's say we what to remove a gender field from steve 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ju9yilr9vem71qzorieh.png)

- Once we run the above command and do a search we see that the gender field has been removed from Steve.

- Sometimes we might we updating something which does not exist, what we do in this scenario is that we add an operator called upsert to add it if it doesn't exist.

- In our case, let's say we what to update a customer called Marry which does not exist in our database, this is how we do it to avoid errors.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d8lvi95i250o2ypvteea.png)

- We also have an operator called **rename** that can be used with an update, Let's rename gender to sex. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3qbzqp6vwset9j5g2tyy.png)

- To remove a document is very easy, let's see by removing Steve.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bk5zfqebd4wl27wy29q3.png)

**conclusion**

Hopefully, this gives you an idea of how to use MongoDB within your application. This is just the database part, you need to get familiar with drivers and packages and all that staff.