DEV Community

Dan Musembi
Dan Musembi

Posted on • Updated on



Image description


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 and download the MSI package of MongoDB.

Image description

  • Go ahead and run that MSI file

Image description

  • Choose custom to change the location for simplicity.

Image description

  • 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

  • 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

  • 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

Running MongoDB

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

Image description

  • 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

  • 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

  • create a user of the database.

Image description 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 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

  • 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

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

Image description

  • 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

  • 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

  • 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

  • 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

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

Image description

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

Image description


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.

Oldest comments (0)

Visualizing Promises and Async/Await 🤓

async await

☝️ Check out this all-time classic DEV post on visualizing Promises and Async/Await 🤓