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.
- Every post contain unique title, description and URL.
- Post can have one or more tags.
- Post has the name of its publisher and total number of likes.
- Post can have comments and the comments must contain username, message, date-time and likes.
But in MongoDB, schema design will have one collection post and has the following structure:
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 :
Example
If you want to create a database with name
To check your currently selected database, use the command db
If you want to check your databases list, use the command show dbs.
Your created database (mydb) is not present in list. To display database, you need to insert at least one document into it.
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.
MongoDB Create Collection
In MongoDB, db.createCollection(name, options) is used to create collection.Syntax:
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.
- 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.
- AutoIndexID: If it is set to true, automatically create index on ID field. Its default value is false.
- Size: It specifies a maximum size in bytes for a capped collection. If capped is true, then you need to specify this field also.
- Max: It specifies the maximum number of documents allowed in the capped collection.
You can check the created collection by using the command show collections.
The following example shows the syntax of createCollection() method with few important options
In MongoDB, you don't need to create collection. MongoDB creates collection automatically, when you insert some document.
Great blog !! thanks for sharing the Valuable information...
ReplyDeleteMean stack online training
Mean stack training in hyderabad