You are on page 1of 8
UNIT — 3 INTRODUCTIONS TO MongoDB 3.1 Introduction + What is MongoDB © MongoDB is an open-source document database that provides high performance, high availability, and automatic scaling. © Mongo DB is a document-oriented database. It is an open-source product, developed and supported by a company named 10gen. ‘© MongoDB is a NoSQL database that stores the data in form of key-value pairs. + Why do you need MongoDB technology? This technology overcame one of the biggest pitfalls of the traditional database systems, that is, scalability. With the ever-evolving needs of businesses, their database systems also needed to be upgraded. MongoDB has exceptional scalability. It makes it easy to fetch the data and provides continuous and automatic integration. Along with these benefits, there are multiple reasons why you need MongoDB: * No downtime while the application is being scaled * Performs in-memory processing + Text search + Graph processing + Global replication + Economical 3.2 WHAT IS A DOCUMENT? ‘A Document is nothing but a data structure with name-value pairs like in JSON. It is very easy to map any custom Object of any programming language with a MongoDB Document. For example: Student object has attributes name, roll no and subjects, where subjects are a List, Document for Student name ABC in MongoDB will be like: { name : "ABC", (°C Language", "C++", "Core Java") 3.3 History of MongoDB The initial development of MongoDB began in 2007 when the company was building a platform as a service similar to window azure. “Window azure is a cloud computing platform and infrastructure, created by Microsoft, to build, deploy and manage applications and service through a global network.” MongoDB was developed by a NewYork based organization named 10gen which is now known as MongoDB Inc. It was initially developed as a PAS (Platform as a Service). Later in 2009, it is introduced in the market as an open source database server that was maintained and supported by MongoDB Inc. The first ready production of MongoDB has been considered from version 1.4 which was released in March 2010. 3.4 The architecture of MongoDB NoSQL Database a i oteliPoat Pere Mongod rel ed —- Database: In simple words, it can be called the physical container for data. Each of the databases has its own set of files on the file system with multiple databases existing on a single MongoDB server. Collection: A group of database documents can be called a collection. The RDBMS equivalent to a collection is a table. The entire collection exists within a single database. There are no schemas when it comes to collections. Inside the collection, various documents can have varied fields, but mostly the documents within a collection are meant for the same purpose or for serving the same end goal. Document: A set of key-value pairs can be designated as a document. Documents are associated with dynamic schemas. The benefit of having dynamic schemas is that a document in a single collection does not have to possess the same structure or fields. Also, the common fields in a collection document can have varied types of data, 3.5 Features of MongoDB These are some important features of MongoDB: © Schema-less Database: It is the great feature provided by the MongoDB. A Schema-less database means one collection can hold different types of documents in it. Or in other words, in the MongoDB database, a single collection can hold multiple documents and these documents may consist of the different numbers of fields, content, and size. It is not necessary that the one document is similar to another document like in the relational databases. Due to this cool feature, MongoDB provides great flexibility to databases. Document Oriented: In MongoDB, all the data stored in the documents instead of tables like in RDBMS. In these documents, the data is stored in fields(key-value pair) instead of rows and columns which make the data much more flexible in comparison to RDBMS. And each document contains its unique object id. Indexing: In MongoDB database, every field in the documents is indexed with primary and secondary indices this makes easier and takes less time to get or search data from the pool of the data. If the data is not indexed, then database search each document with the specified query which takes lots of time and not so efficient. Scalability: MongoDB provides horizontal scalability with the help of sharding. Sharding means to distribute data on multiple servers, here a large amount of data is partitioned into data chunks using the shard key, and these data chunks are evenly distributed across shards that reside across many physical servers. It will also add new machines to a running database. Replication: MongoDB provides high availability and redundancy with the help of replication, it creates multiple copies of the data and sends these copies to a different server so that if one server fails, then the data is retrieved from another server. Aggregation: It allows to perform operations on the grouped data and get a single result or computed result. It is similar to the SQL GROUPBY clause. It provides three different aggregations i.e, aggregation pipeline, map-reduce function, and single-purpose aggregation methods High Performance: The performance of MongoDB is very high and data persistence as compared to other databases due to its features like scalability, indexing, replication, etc. 3.5.1 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. The 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. 3.6 Mapping relational database to MongoDB RDBMS MongoDB Row ) Document Linking and Embedding Relationships -—_——> Documents Mapping chart Difference between Relational Databases to MongoDB:- Relational Databases Relational database Table based Row based Column based Group By Support for triggers SQL injection vulnerability Not suitable for hierarchical data storage Supports SQL © Collection: MongoDB Non-relational and document-oriented database collection based and key-value pair Document based Field based Aggregation No Support for triggers Unaffected by SQL injection suitable for hierarchical data storage Supports JSON ‘ollections in MongoDB are equivalent to the tables in RDBMS. © Documents:-Documents in MongoDB is equivalent to the rows in RDBMS. Fields:-Fields in MongoDB is equivalent to the columns in RDBMS. Note:-Fields (key and value pairs) are stored in document; documents are stored in collection and collections are stored in database. Create In MongoDB, there is no need to explicitly create the collection structure (as we do for tables using a CREATE TABLE query). The structure of the document is automatically created when the first insert occurs in the collection. However, you can create an empty collection using createCollection command. SQL: CREATE TABLE ‘posts’ (‘id* int(11) NOT NULL AUTO_INCREMENT, ‘post_text’ varchar(500) NOT NULL, “user_name* varchar(20) NOT NULL, *post_privacy* varehar(10) NOT NULL, *post_likes_count’ int(11) NOT NULL,PRIMARY KEY (“id’)) MongoDB: db.createCollection("posts") Insert To insert a document in MongoDB, we use the insert method which takes an object with key-value pairs as its input. The inserted document will contain the autogenerated _id field. However, you can also explicitly provide a 12-byte value as _id along with the other fields. SQL: INSERT INTO ‘posts’ ‘id’ ,'post_text’ ,'user_name’ ,"post_privacy’ , *post_likes_count") VALUES (NULL, "This is a sample post’, ‘mark’, ‘public’, '0'); MongoDB: db.posts.insert( { user_name:"mark", post_text:"This is a sample post", post_privacy:"public", post_likes_count:0} ) There is no Alter Table function in MongoDB to change the document structure. As the documents are dynamic in the schema, the schema changes as and when any update happens on the document. Read MongoDB uses the find method which is equivalent to the SELECT command in SQL. The following statements simply read all the documents from the posts collection, SQL: SELECT * FROM ‘posts’ MongoDB: db posts.find() Remove Removing documents is quite simple and similar to SQL. SQL: DELETE FROM posts WHERE user_name="'mark' MongoDB: db.posts.remove({user_name:"mark"}) 3.7 Table VS Collections Tables in Oracle are similar to MongoDB’s ‘Collections’ The table in Oracl A table in Oracle is made up of a fixed number of columns for any number of rows. Every row in a table has the same columns. Example of an Oracle table: EMP EMPID NAME CITY 1 Smith Karachi 2 Adam Lahore 3 Jim Wah Cantt 4 Ken Quetta Create table syntax: CREATE TABLE EMP (EMPID NUMBER(S5),NAME, VARCHAR2(20),CITY VARCHAR2(25)); Insert syntax: INSERT INTO EMP VALUES (1,’SMITH’,’KARACHI’); INSERT INTO EMP VALUES (2,’ADAM’,’ LAHORE’); INSERT INTO EMP VALUES (3,’JIM’,'WAH CANTT’); INSERT INTO EMP VALUES (4,’KEN’, KARACHI’); Select syntax: Select * from EMP; In the above example, the table is ‘EMP’, with 4 rows. All 4 rows have a fixed number of columns EMPID, NAME, and CITY. Collection in MongoD! © A collection in MongoDB is made up of documents. The concept of Documents is similar to rows in a table, but it’s not identical. A document can have its own unique set of columns. In MongoDB, columns are called fields, © So, in MongoDB, fields are defined at the document level (or we can say in Oracle lingo that columns are defined at the row level), whereas in Oracle the columns are defined at the table level. That is actually the main difference between Oracle's Table and Mongo’s collection among other subtle differences such as collections are schema-less, whereas Table in Oracle has to be in some schema. Example of a MongoDB Collection: db.EMP.insert( {EMPID: ‘1’,NAME: ‘Smith’, CITY: ‘Karachi’}) db.EMP.insert( {EMPID: Designation: ‘CTO"}) db.EMP.insert({EMPID: db.EMP.insert({EMPID: ‘4’, NAME: ‘Ken’}) > db.EMP.find() { “_id” : ObjectId(“55d44757283d7d463aec4cc1”), “EMPID’ “NAME” : “Smith”, “CITY” : “Karachi” } (Lid? : Objectld(*55d44757283d7d463aec4ee2”), “EMPID” : “2”, “NAME” : “Adam”, “CITY” : “Wah Cantt”, “Designation” : “CTO” } { “_id” : ObjectId(“55d44757283d7d463aec4cc3”), “EMPID” : “3”, “NAME” : “Jim”, “Designation” : “Technician” } { “_id” : ObjectId(“55d44757283d7d463aec4cc4”), “EMPID” : “NAME” : “Ken” } In the above example, first we inserted 4 documents into collection ‘EMP’ Notice that all 4 documents have different number of columns. db.EMP.find()

You might also like