MongoDB — Getting started

Hello and welcome to a MongoDB with this guide I was help you get started with MongoDB and its basics.

First lets discuss what exactly a NoSQL database is. If you have worked with a RDBMS database you can see some comparisons to it.

Database

A database holds information for applications whether they have web, desktop or mobile based. Databases will hold information that is required for an application such as users information, records or anything that the application needs.

Collection

A collection contains all the information for a specific set of data, for example you can have a collection called users which holds the users username, password and email while also having another collection that holds purchases by that customer but they are within the same database.

Structure

With a RDBMS database you have to state a set structure that indicates the column name and its data type, The information being inserted into the database must comply by that structure but with a NoSQL database you do not need to do this as information being inserted does not need to comply by a set structure, so a user can have more fields than another user and this wouldn’t cause a constraint error.

MongoDB

Installation

Go to the following link to download MongoDB:

Choose the propere MSI file for you operation system.

I recommand creating a folder called MongoDB inside C: drive or whatever your main direcutory is on your system but remeber what the default installtion path is and installing the files there are this will make the following commands easier:

  1. Follow the installation instructions
  2. go to the installation folder.
  3. create a folder called “data” and inside that create a folder called “db”. Here we will place the database files.
  4. create a folder called “log” in the same folder as “data”
  5. open cmd as administrator inside the bin folder and run the following command:

This will allow you start MongoDB as a service.

6. Type “net start mongo” into the cmd terminal and wait for it too start up.

7. you can type in “net stop mongo” to stop the mongo service.

Mongo Shell

Now since everything is up and running at this point we can get started with creating our MongoDB database.

  1. Type “mongo” into the cmd.

Note: you must start cmd inside MongoDB\bin folder for this to work / all commands are to be written without qoutation marks around them.

Simple commands

All commands are to be inside the mongo shell.

“help” = displays helpful information.

“show dbs” = will print list of all databases on the server.

“use dbname” = creates a database with a specfied name or uses a already created database with that name.

“show collections” = prints list of collections inside the current database.

“show users” = prints a list of users for the current database.

“show roles” = prints a list of all roles.

“show profile” = prints the most recent operations or transaction used against the database.

“show databases” = prints a list of all available databases.

Database creation

Lets create our own database.

1. Type “use store” this will create a database called store but you can name it whatever you want.

2. Type “db.createCollection(‘customers’);” this will create a collection called customers that will hold our information.

User creation

lets create a user for someone else to log into.

  1. type “db.createUser({user:”john”, pwd:”1234”, roles: [“readWrite”, “dbAdmin”]});”

This would create a user called john with the password of 1234 and the ability perform read and write operation against a database and give the admin privileges.

Insert information into database

lets insert some information into the database.

this above command would insert information into the customers collection. first_name and last_name would be examples of strings

age is double.

Memberships is an array.

address is an object.

car is an array of objects.

update information

lets update the information we just inserted.

this would update the age of anyone with the name of john and set it too 22. If you do not specify the $set it will clear the information for the user with and john would only have age.

delete information

lets delete some information.

this command would remove any with the first name john from the database.

Thank you for reading my blog post about MongoDB.