The MongoDB is an open-source document database and leading NoSQL database. MongoDB is written in C++. This tutorial will give you great understanding on MongoDB concepts needed to create and deploy a highly scalable and performance-oriented database. MongoDB features are flexible data models that allows the storage of unstructured data. This provides full support indexing, replication, capabilities and also user friendly APIs. The MongoDB is a multipurpose dataset that is used for modern application development and cloud environments. This scalable architecture enables us to handle system demands and also adding more nodes to distribute the load. 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.
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.
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.
| 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) |
| Database Server and Client | |
| mysqld/Oracle | mongod |
| mysql/sqlplus | mongo |
1. Schema Less - MongoDB is a document database in which one collection holds different documents.
2. Number of fields, content and size of the document can differ from one document to another.
3. Structure of a single object is clear, No complex joins and Turning.
4. Deep query-ability. MongoDB supports dynamic queries on documents using a document-based query language that's nearly as powerful as SQL.
5. Ease of scale-out − MongoDB is easy to scale.
6. Conversion/mapping of application objects to database objects not needed.
7. Uses internal memory for storing the (windowed) working set, enabling faster access of data.
1. Document Oriented Storage - Data is stored in the form of JSON style documents.
2. Index on any attribute
3. Replication and high availability
4. Auto-Sharding and Rich queries
5. Fast in-place updates
6. Professional support by MongoDB
1. Big Data and Data Hub
2. Content Management and Delivery
3. Mobile and Social Infrastructure
4. User Data Management
MongoDB provides two types of data models
1. Embedded Data Model : you can have (embed) all the related data in a single document, it is also known as de-normalized data model.
2. Normalized Data Model : you can refer the sub documents in the original document, using references.
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 : use DATA_BASE_NAME
Example : use jstdb
MongoDB db.dropDatabase() command is used to drop a existing database.
Syntax : db.dropDatabase()
Example : db.dropDatabase()
Syntax : db
Example : db
MongoDB db.createCollection(name, options) is used to create collection.
Syntax : db.createCollection(name, options)
Example : db.createCollection("myjst")
MongoDB's db.collection.drop() is used to drop a collection from the database.
Syntax : db.COLLECTION_NAME.drop()
Example : db.myjst.drop()
| String | This is the most commonly used datatype to store the data. String in MongoDB must be UTF-8 valid. |
| Integer | This type is used to store a numerical value. Integer can be 32 bit or 64 bit depending upon your server. |
| Boolean | This type is used to store a boolean (true/ false) value. |
| Double | This type is used to store floating point values. |
| Min keys | This type is used to compare a value against the lowest BSON elements. |
| Max keys | This type is used to compare a value against the highest BSON elements. |
| Arrays | This type is used to store arrays or list or multiple values into one key. |
| Timestamp | ctimestamp. This can be handy for recording when a document has been modified or added. |
| Object | This datatype is used for embedded documents. |
| Null | This type is used to store a Null value. |
| Symbol | This datatype is used identically to a string; however, it's generally reserved for languages that use a specific symbol type. |
| Date | This datatype is used to store the current date or time in UNIX time format. You can specify your own date time by creating object of Date and passing day, month, year into it. |
| Object ID | This datatype is used to store the documents ID. |
| Binary Data | This datatype is used to store binary data. |
| Code | This datatype is used to store JavaScript code into the document. |
| Regular Expression | This datatype is used to store regular expression. |
To insert data into MongoDB collection, you need to use MongoDB's insert() or save() method.
Syntax : db.COLLECTION_NAME.insert(document)
Example : db.myjst.drop()
If you need to insert only one document into a collection you can use this method.
Syntax : db.COLLECTION_NAME.insertOne(document)
Example : db.myjst.drop()
You can insert multiple documents using the insertMany() method. To this method you need to pass an array of documents.
Syntax : db.COLLECTION_NAME.insertMany(document)
Example : db.myjst.drop()