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.
Full cloud-based developer data platform
Flexible document schema
Widely supported and code-native data access
Powerful querying and analytics
Easy horizontal scale-out with sharding
Full technical support and documentation
- First, you need to heed to MongoDB and download the MSI package of MongoDB.
- Go ahead and run that MSI file
- Choose custom to change the location for simplicity.
- 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.
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
- 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.
- Now we should be able to run the service with the below command
- We are going to be working from Mongo shell and we can do that by typing the command mongo in the bin directory.
- We are now on the shell, you can type cls to clear everything out.
Let's now create a document database by running the following command, use mycustomers (mycustomers) is the database name.
- create a user of the database.
- 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.
- 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.
- 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.
- make the database a bit neat by running command , db.customers.find().pretty();
- 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.
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.
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
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.
- We also have an operator called rename that can be used with an update, Let's rename gender to sex.
- To remove a document is very easy, let's see by removing Steve.
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.