MongoDB - daTa ModeLLinG Concepts: SQL - Where all to UsE ?

Data Modeling

Data modeling (data modelling) is the analysis of data objects and their relationships to other data objects. Data modeling is often the first step in database design and object-oriented programming as the designers first create a conceptual model of how data items relate to each other. Data modeling involves a progression from conceptual model to logical model to physical schema.

MongoDB Data Modeling

In MongoDB, data has a flexible schema. It is totally different from SQL database where you had to determine and declare a table's schema before inserting data. MongoDB collections do not enforce document structure. The main challenge in data modeling is balancing the need of the application, the performance characteristics of the database engine, and the data retrieval patterns.

Following things must be consider while designing MongoBD data models

  • Always design schema according to requirements.
  • Perform join on write operations only not on read operations.
  • Objects which you want to use together, should be combined into one document. Otherwise they should be separated.
  • Optimize schema for more frequent use cases.
  • Do complex aggregation in the schema.
  • You should duplicate the data but in a limit, because disc space is cheaper than compute time.
Lets see the example of website. Website has following requirements:
  1. Every post contain unique title, description and URL.
  2. Post can have one or more tags.
  3. Post has the name of its publisher and total number of likes.
  4. Post can have comments and the comments must contain username, message, date-time and likes.
For the above example, minimum three tables are required in RDBMS.

But in MongoDB, schema design will have one collection post and has the following structure:
 {
_id: POST_ID
title: TITLE_OF_POST,
description: POST_DESCRIPTION,
by: POST_BY,
url: URL_OF_POST,
tags: [TAG1, TAG2, TAG3],
likes: TOTAL_LIKES,
comments: [
{
user: 'COMMENT_BY',
message: TEXT,
datecreated: DATE_TIME,
like: LIKES
},
{
user: 'COMMENT_BY',
message: TEST,
dateCreated: DATE_TIME,
like: LIKES
}}}
So while showing the data, in RDBMS you need to join three tables and in MongoDB, data will be shown from one collection only.

Lets see how to create Database in MongoDB

There is no create database command in MongoDB. MongoDB use DATABASE_NAME is used to create database. The command create new database if it doesn't exist, otherwise it will return the existing database.
Syntax :
use DATABASE_NAME
Example
If you want to create a database with name , then use DATABASE statement would be as follows :
>use mydb
To check your currently selected database, use the command db
>db
If you want to check your databases list, use the command show dbs.
>show dbs
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":"SCTPL"})
>show dbs
In MongoDB default database is test. If you didn't create any database, then collections will be stored in test database.

The dropDatabase command is used to drop a database. It also deletes the associated data files. It operates on the current database.
db.dropDatabase()

MongoDB Create Collection

In MongoDB, db.createCollection(name, options) is used to create collection.
Syntax:
db.createCollection(name, options)
Name: is a string type, specifies the name of the collection to be created.
Options: is a document type, specifies the memory size and indexing of the collection. It is an optional parameter.
Following is the list of options that can be used.
  1. Capped: If it is set to true, enables a capped collection. Capped collection is a fixed size collecction that automatically overwrites its oldest entries when it reaches its maximum size. If you specify true, you need to specify size parameter also.
  2. AutoIndexID: If it is set to true, automatically create index on ID field. Its default value is false.
  3. Size: It specifies a maximum size in bytes for a capped collection. If capped is true, then you need to specify this field also.
  4. Max: It specifies the maximum number of documents allowed in the capped collection.
Let's take an example to create collection. In this example, we are going to create a collection name SCTPL.
>use test
switched to db test
>db.createCollection("SCTPL")
{ "ok" : 1 }
>
You can check the created collection by using the command show collections.
 >show collections
The following example shows the syntax of createCollection() method with few important options
>db.createCollection("SCTPL_COL", { 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.

Do you want to learn database Programming?


Comments

Post a Comment

Popular posts from this blog

How E-commerce Sites can Increase Sales with Pinterest?

Every thing U can do with a Link-List + Programming_it_in_JaVa

Test Your SQL Basics - Part_1