Skip to main content

Mongo DB Part 1

 






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

 

 Where to Use 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:                                        

                           db.createCollection(name, options)


 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

Popular posts from this blog

What is A-Frame.io

What is aframe?? Before I get this I like to ask from you, Did you ever have to get a virtual reality? Someones are Yes! or Not!, Do you like to get that experience? So here it is. A-Frame is a web framework for building virtual reality (VR) experience. This web frame is an open-source web framework that can develop many things. It is primarily maintained by Mozilla and WebVR community. But originally from Mozilla. A-Frame was developed to be an easy but powerful and easy way to develop VR content. As an open-source project, A-Frame has grown to be one of the largest and most welcoming VR communities. A-Frame is based on top of HTML, simple to get started. But A-Frame is not just a 3D scene graph or markup language. It is an entity-component system framework for  three.js  where developers can create 3D  WebVR  scenes using HTML. Nowadays HTML provides a familiar tool for web developers and designers while incorporating a popular game development pattern use...

SDLC (System Development Life Cycle)

මේ SDLC කියන්නේ මචන් cook book එකක් !! ඒ කියන්නේ හරියට manual guide book එකක් වගේ සීන් එකක් හැබැයි ඔව් cook book එකක් කියන්නේ කෑම වට්ටෝරු පොතක් නේ. දැන් මෙහෙම හිතපන් උබට ඕනේ වෙනවා පරිපුවක් හදා ගන්න. නැ ඒ පරිප්පු නෙමේ කන පරිප්පු. මේ පරිප්පු වේ recipe එක විදිහට තමා අපි හදන්නේ ඒ කියන්නේ 2 කුට නම්  ලුණු තේ හැදී භාගයයි, පරිප්පු කොප්පයි, මිරිස් කරල් 2 යි කහ තේ හැදී කාලයි ඇයි ඒ?  අපිට ලුණු මේස හැන්දක් මිරිස් කරල් 10 විතර ගන්න තහනම්ද? නැ මචන් තහනම් නැ හැබෙයි එහෙම හදපල්ලා කාගෙවත් බාධාවක් නෑ. මේකයි මචන් අපිට කලින් පරම්පරාවේ මිනිස්සු මෙන්න මේ පරිප්පුව හදන විදිහ වැරදිලා වැරදිලා වැරදිලා ක්‍රමවේදයක් හොයාගෙන තියෙනවා හෝ හෙවිලා තියෙනවා අපිට වඩාත් හොදින් කොලිටියට පරිප්පුව හදාගන්න :O ඉතින් මේ SDLC පරිප්පුව  "මෘදුකාංග වැඩිදියුණු කිරීමේ ජීවන චක්‍රයක්". ඒ කියන්නේ හැමදාම පරිප්පුව හදනවා ඉවර වෙන්නේ නෑ. හරියට සංසාර චක්රේ වගේ. මෘදුකාංග ජීවන චක්‍රය ඉවර වෙන්නේ නෑ කියන්නේ බන්ස්, අපේ සංසාර ච්ක්රේ  ඉපදෙනවා, හොද නරක කරනවා, මැරෙනවා, ආය ඉපදෙනවා, හොද නරක කරනවා,මැරෙනවා, අයේ ඇති ඇත...

Dark Web

                                    Dark Web කොහොමද යාලුවනේ, අද මම කතාකරන්න යන්නෙ ගොඩක් දෙනෙක්   වැඩිය දැනුවත් නැති ,ඒ වුනාට අපි අනිවාරෙන්ම දැනගත යුතු   මාතෘකාවක්   ගැන. ඉතින් අපි බලමු මොකක්ද ඒ කියලා. මොකක්ද මේ  Dark web කියලා කියන්නෙ....  D ark web කියන්නෙ අන්තර්ජාලයේම කොටසක් නමුත් dark web කියන එක අපිට search engine එකක් හරහා බලාගන්න බැහැ.ඒ සදහා අපිට anonymizing browser එකක් භාවිතා කරන්න වෙනවා.එහෙම භාවිතා කරන browser එකක් තමයි Tor කියන්නෙ. dark මේක  ගොඩක් භාවිත වෙන්නෙ criminal activity වලට ඒ නිසාම hotbed of criminal activity කියලත් මේ dark web එක හදුන්වනවා.   ක්‍රෙඩිට් කාඩ් අංක , සියලු ආකාරයේ මත්ද්‍රව්‍ය , තුවක්කු , ව්‍යාජ මුදල් , අනවසරයෙන් නෙට්ෆ්ලික්ස් ගිණුම්   වලට සහ වෙනත් පුද්ගලයින්ගේ පරිගණකවලට ඇතුළු වීමට උදව් වන මෘදුකාංග මිලදී ගැනීම ,පරිගණකවලට පහර දීමට හැකර්වරුන් කුලියට ගැනීම,   පරිශීලක නාම සහ මුරපද ම...