Learn Mongo db
- Basic Overview
- SetUp environment/Installation
- DataTypes
- Schema Design
- Create/Drop Database
- Create/Drop Collection
- Insert/update/delete document
- Query on Document
- Indexing, limit and sorting
- Backup and restore database
Basic Overview
MongoDB is an open-source/cross platform (Windows, Linux etc) document database that is a NoSql JSON format database. A NoSQL database means it does not have any relationships (non-relational database). It is a document-oriented database.
Like JSON format it is written in C++. The purpose of this database is to create and deploy a highly scalable and performance-oriented database. Stable release: 3.4.6 / 5 July 2017; 30 days ago Initial release: February 2009, 11
Compare with Relational DB
| Relational DB | Mongo DB |
1 | Database is called Database | Databse |
2 | Table is called table | Collection |
3 | Any records are called row | Document |
4 | Column is called column | Field |
5 | Table Join with resulting data between two or more tables is called table join | Embeded Documen |
6 | Primary Key is unique and not null value | Primary Key (Default key _id) it is hexadecimal value (12 byte)12 byte: 4 byte time stamp, 3 byte machine code, 2 bytes process id and 3 byes for incremeted process |
SetUp environment/Installation
Download path
https://www.mongodb.com/download-center#community
Install properly, after installation you will see:
C:\Program Files\MongoDB\Server\3.4\bin,
Then from here you need to do -> Shift with right click,
Now click on Open command window here. And type mongod.exe.
I hope you will see below Window.
Now you will see the bottom number waiting for connection.
So now again do it with right shift click as a given place. And type mongo.exe.
Remember - do not close first command window
You will see connection has been accepted/ in first cmd window
Now your installation has been completed.
For your confirmation just type : show dbs command,
You will see your default database names; for me it shows a few database names,
In this window admin and local is default database, and I have created Demo, MovieStore, and School.
DataTypes
Now we will learn about datatypes in mongo db
MongoDB accepts many datatypes. There are following datatypes,
Double | String | ObjectId | Array | BinaryData | Boolean | Date | TimeStamp | Null | Int |
| | | | | | | | | |
ObjectId
ObjectIds are unique, fast to generate, and ordered. ObjectId values consist of 12 bytes, where the first four bytes are a timestamp that reflect the ObjectId’s creation. Specifically,
- a 4-byte value representing the seconds since the Unix epoch,
- a 3-byte machine identifier,
- a 2-byte process id, and
- a 3-byte counter, starting with a random value.
Schema Design
In MongoDB, data is a flexible schema. It is we can say different from SQL database or any other relational database, where you need to determine and declare a table's schema before inserting data. MongoDB collections/tables do not force you to create document structure/schema.
Let me explain with a simple example,
Suppose you have a client who needs a database design for his website.
Suppose he wants a website with the following requirements,
- Every post must be distinct (and it contains unique title, description and url).
- Every post can have zero, one, or more tags.
- Every post can have the name of its publisher and total number of likes/dislikes.
- And for every post it can have zero or more comments and the comments must contain its user name, message, data-time and likes/dislikes.
So for the above requirement in relational database,we need minimum three tables.
But you can see in MongoDB, how much it fulfills the requirement in one collection post and has the following structure:
- {
- _id: int
- title: string
- description: string
- by: string
- url: string,
- tags: [],
- Totallikes: int,
- TotalDislikes: int,
- comments: [{
- user: string,
- message: Text,
- datecreated: DateTime
- like: int,
- dislikes: int,
- }, {
- user: string,
- message: Text,
- dateCreated: DateTime
- like: : int,
- dislikes: int
- }
- }
- }
Create/Drop Database
This following command is used to create a new database.
Syntax
use DATABASE_NAME
If the database already exists/created, it will return the existing database.
In the example to demonstrate how a database is created in MongoDB, in the following example, we are going to create a database "appeal".
then use the following command
>use appeal
Swithched to db apeal
To check the currently selected database, use the command db:
>db
appeal
To check the database list, use the command show dbs:
AS I have already explained in the above window
>show dbs
In my window i have a few db.
- demo,
- moviestore,
- school,
- admin,
- appeal,
- local
Here, your created database "appeal "is not present in the list, so that you need to insert at least one document into it to display database,
>db.appeal.insert({"appealName":"test1"})
WriteResult({ "nInserted": 1})
For the drop database which you have created this command is used to drop a database. It also deletes the associated data files. It operates on the current database.
Syntax
dropDatabase()
This syntax will delete the selected database. In the case of if you have not selected any database, it will delete the default "test" database.
Please see the error, because I did not write function name as case sensitive
So please use proper case for that.