Hi every one
today we are going to talk about MongoDB.Most of the time I will continue this
article how can we create a simple full
project with frontend using mongodb .So
this will be the first article of the series.I hope to make it very simple and
clear . Let’s move to the content.
What is
a DataBase?
Data base is
simply a collection of data.If we talk
using technical terms in mongodb, Database is a physical container for
collections. Each database gets its own set of files on the file system. A
single MongoDB server typically has multiple databases.
MongoDB
MongoDB is a
NoSQL Document type database . NoSQL stands for “Not Only SQL”.As well as
mongodb is a non-Relational database.NoSQL is fundamentally different than
relational database .Mongo DB is shameless language that means no tables and
columns.There are many advantages than
RDBMS. MongoDB is a cross-platform, document oriented database that provides,
high performance, high availability, and easy scalability. MongoDB works on
concept of collection and document.
What is
Document Data Model
In here data
stored in documents and use syntax like
BSON and Json. Here database values can contain
strings ,numbers ,arrays, objects,
ObjectId and Null.Documents
stored in collections .
What is a Collection?
Collection
is a group of MongoDB documents. It is the equivalent of an RDBMS table. A
collection exists within a single database. Collections do not enforce a
schema. Documents within a collection can have different fields. Typically, all
documents in a collection are of similar or related purpose.
What is a
Document?
A document
is a set of key-value pairs. Documents have dynamic schema. Dynamic schema
means that documents in the same collection do not need to have the same set of
fields or structure, and common fields in a collection's documents may hold
different types of data. The following table shows the relationship of RDBMS
terminology with MongoDB.
|
RDBMS |
MongoDB |
|
Database |
Database |
|
Table |
Collection |
|
Tuple/Row |
Document |
|
column |
Field |
|
Table Join |
Embedded Documents |
|
Primary Key |
Primary key(Default key_id provided by
mongodb itself) |
|
Mysql/Oracle |
mongodb |
|
Mysql/sqlplus |
mongo |
Advantages
of MongoDB over RDBMS
· Schema less: MongoDB is a document database in which
one collection holds different documents. Number of fields, content and size of
the document can differ from one document to another.
· Structure of a single object is clear.
· No complex joins.
· Deep query-ability. MongoDB supports dynamic queries on
documents using a document-based query language that's nearly as powerful as
SQL.
· Tuning.
· Ease of scale-out: MongoDB is easy to scale.
· Conversion/mapping of application objects to database
objects not needed
. · Uses internal memory for storing the
(windowed) working set, enabling faster access of data.
Why Use MongoDB?
· Document Oriented Storage: Data is stored in the form of
JSON style documents
. · Index on any attribute
· Replication and high availability
· Auto-shading
· Rich queries
· Fast in-place updates
· Professional support by MongoDB
· Big Data
· Content Management and Delivery
· Mobile and Social Infrastructure
· User Data Management
· Data Hub
To install
MongoDB on Windows, first download the latest release of MongoDB from http://www.mongodb.org/downloads.
Let’s see
how to work with mongodb.
Use Command
MongoDB use
DATABASE_NAME is used to create database. The command will create a new
database if it doesn't exist, otherwise it will return the existing database.
Syntax
Basic syntax of use DATABASE statement is
as follows:
use DATABASE_NAME
Example
If you want to create a database with name ,
then use DATABASE statement would be as follows:
>use mydb
switched to db mydb
To check
your currently selected database, use the command db
>db mydb
If you want
to check your databases list, use the command show dbs.
>show dbs
Local 0.78125GB
test 0.23012GB
Your created
database (mydb) is not present in list. To display database, you need to insert
at least one document into it.
>db.movie.insert({"name":"subjects"})
>show dbs
local
0.78125GB
mydb
0.23012GB
test
0.23012GB
In MongoDB default database is test. If you
didn't create any database, then collections will be stored in test database.
The
dropDatabase() Method
MongoDB db.dropDatabase() command is used
to drop a existing database.
Syntax
Basic syntax of dropDatabase() command
is as follows:
db.dropDatabase()
This will
delete the selected database. If you have not selected any database, then it
will delete default 'test' database.
Example
First, check the list of available
databases by using the command, show dbs.
>show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
>
If you want
to delete new database , then dropDatabase() command would be as follows:
>use mydb
switched to db mydb
>db.dropDatabase()
>{
"dropped" : "mydb", "ok" : 1 }
>
Now check
list of databases.
>show dbs
Local 0.78125GB
test
0.23012GB
>
The
createCollection() Method
MongoDB
db.createCollection(name, options) is used to create collection.
Syntax
Basic syntax of createCollection()
command is as follows:
In the command, name is name of collection to
be created. Options is a document and is used to specify configuration of
collection.
Examples
Basic syntax of createCollection() method without
options is as follows:
>use test switched to db test
>db.createCollection("mycollection")
{ "ok" : 1 } >
You can check the created collection by using
the command show collections.
>show
collections
mycollection
system.indexes
The
following example shows the syntax of createCollection() method with few
important options:
>db.createCollection("mycol", {
capped : true, autoIndexID : true, size : 6142800, max : 10000 } )
{ "ok" : 1
}
>
In MongoDB,
you don't need to create collection. MongoDB creates collection automatically,
when you insert some document.
>db.subjects.insert({"name"
: "maths"})
>show collections
mycol
mycollection
system.indexes
math
>
So this is
going to be the end of this article. We will meet soon with MongoDB part2.
-Tyni Egoda Gedarage -

Comments
Post a Comment