In this tutorial we will be Starting With MongoDB NoSQL Database, understanding the basic concept.
The MongoDB
MongoDB is a NoSQL database, MongoDB is known as document database and it uses the JSON-like syntax. It is much different than our relational database like MySQL. MongoDB is an Open Source database written in C++. MongoDB stores data in flexible, JSON-like documents, meaning fields can vary from document to document and data structure can be changed over time.
Start the MongoDB
After installing it go to the MongoDB folder you will find the bin folder. In that folder, we will use only two files i.e. mongod.exe and mongo.exe
First start the mongod.exe file let it be running and then start the mongo.exe, As it will lead to you on a terminal to perform an action. It will show connecting to: mongodb://127.0.0.1:27017 this means it is connected to the port:27017.
MongoDB Commands
so our first command will be showing databases
> show dbs
admin 0.000GB
local 0.000GB
This will sow our local directory or our drives that been used by the MongoDB. As we are starting and we haven’t used any drive for a database that’s why it shows the 0.000GB bytes. For this tutorial, we will be using admin.
So for switching into admin database, we will use the command.
> use admin
switched to db admin
to view collections in that current database, we will use the command
> show collections
Create Database
> use company
switched to db company
It automatically creates the database with the name ‘company’. The one thing in the MongoDB is it don’t save the database or the collection unless we insert any document or data in it.
Let’s create a collection in database bu the following command:
> db.user
company.user
till now it will not save the database because we haven’t insert any document or data. As you can see if we count the collection by using command it will ‘0’
> db.user.count()
0
Insert Data
> db.user.insert({name:"tony hook", gender:"male", user_no:"10001"})
WriteResult({ "nInserted" : 1 }) //response
For inserting the data we will use insert method. The data are stored in JSON format i.e {key:"value",key:"value"} (key-value) pair.
Note : We can save the number in both format i.e ‘string’ and ‘integer’
Find Record
> db.user.find()
{ "_id" : ObjectId("59411e4a0a7668e9cd7ea856"), "name" : "tony hook", "gender" : "male", "user_no" : "10001" }
To find any data we have to determine the collection name, without the collection name it throws an error. As you can see in the collection, the MongoDB gives a unique id to every document/Data by default which is called the primary key.
If you want to prettify the JSON structured data we can use the inbuilt methods .forEach(printjson)
this will convert the data into prettified human readable JSON data.
> db.user.find().forEach(printjson)
{
"_id" : ObjectId("59411e4a0a7668e9cd7ea856"),
"name" : "tony hook",
"gender" : "male",
"user_no" : "10001"
}
JavaScript Console
As the MongoDB performs the Javascript operations, So through the command pad, we can insert data using the javascript language.
so first we will create an empty object and will assign some data to the variables, we will be using an array for this collection and then after we will save the data to the database.
> var data={}
> data.name="Geeks trick"
Geeks trick
> data.user = ["Rehmaan","Ali"]
[ "Rehmaan", "Ali" ]
> data.articles = {}
{ }
> data.articles.MongoDB = "MongoDb Blog post"
MongoDb Blog post
> data.articles.AngularJS = "Angularjs Blog post"
Angularjs Blog post
> db.user.save(data)
WriteResult({ "nInserted" : 1 })
in the MongoDB we don’t have to define any schema i.e not having the structured data. We can assign a collection to an another collection.
After assigning the values we can save the data by db.user.save(data)
It will automatically assign a new unique ID to this JSON data. We can see the saved data by previous command i.e:
> db.user.find().forEach(printjson)
{
"_id" : ObjectId("59411e4a0a7668e9cd7ea856"),
"name" : "tony hook",
"gender" : "male",
"user_no" : "10001"
}
{
"_id" : ObjectId("594125dd0a7668e9cd7ea857"),
"name" : "Geeks trick",
"user" : [
"Rehmaan",
"Ali"
],
"articles" : {
"MongoDB" : "MongoDb Blog post",
"AngularJS" : "Angularjs Blog post"
}
}
Get Some More Mongo HERE !