1. What do you understand by MongoDB?
MongoDB is the most famous NoSQL, distributed document-based database system that is used by millions of developers and companies across the globe to create innovative products and deliver services. The data model used by MongoDB is based on the JSON as Document referred to as BSON format in which MongoDB stores its datasets in the form of Collection, Documents. To query the data from the datasets MongoDB provides a Powerful, expressive query language that can be used to fetch the data at any level of Document nesting. MongoDB provides the high availability of data by maintaining the same set of data on multiple replica sets and automatic failover. The core functionality of MongoDB is its Horizontal Scalability in which the nodes can be added to the MongoDB cluster per requirement which requires no downtime. MongoDB Atlas provides fully managed cloud database services on AWS, Azure, and Google Cloud cloud service providers which guarantee high availability, scalability, and maintains security compliance.
2. Why MongoDB and why not the other databases such as MySQL?
The traditional database management system follows the row/column data model and the millions of data are stored in multiple tables which maintain the relationship with each other using primary key and foreign key so to scale such huge data is a big challenge and difficult task as well. In RDBMS first, we need to spend hours designing the structure of the table before you perform any development activities but this is not the case in MongoDB due to its flexible structure. The query performance of RDBMS is high compared to MongoDB because data is present in multiple tables and joining huge tables and fetching records takes a high amount of CPU and other system resources.
These issues are resolved in MongoDB. The following chart shows the performance chart between MongoDB and SQL.
3. What is the data model of MongoDB and how is it different from RDBMS?
MongoDB stores the data in the form of JSON like Document in MongoDB Collection. The MongoDB Collections are stored in the Database and contain the Documents. The MongoDB collections are similar to a table in the RDBMS and Documents are similar to the records of a table but unlike the table fields which has the same data type, a MongoDB Document field can have different data types and for any change in the structure of the Collection, we just need to update the Document in a new structure.
4. What are the key features of MongoDB?
The following are some of the important features of MongoDB.
- Indexing: Indexes are used to boost the performance of queries.
- Schemaless: This feature makes it a flexible database to query any depth of Document nesting.
- Sharding: Using Sharding MongoDB, distribute the big datasets over multiple nodes for high throughputs.
- Replication: Through Replication the copy of data is replicated over multiple nodes to provide high redundancy.
5. MongoDB best usages for?
MongoDB is the best usages for the following use case.
- MongoDB is best suitable for handlining the Web application.
- It is used for the Content management of semi-structured data.
- It can be used to perform real-time analysis on the datasets also using MongoDB high-speed logging can be done.
- MongoDB can be used for caching and provides high scalability for sectors such as Healthcare Telecom, finance, Gaming, Media, and so on.
6. MongoDB is not good for?
The following are some of the use cases on which MongoDB is not suitable.
- MongoDB is not suitable for high transactional applications.
- It is not best suitable for the problems which require SQL.
- Like RDBMS, MongoDB does not support the triggers.
- The Disk cleanup activity is not performed automatically in MongoDB hence a manual intervention is required.
- Like RDBMS, MongoDB is inadequate in preventing data integrity errors or data anomalies.
7. What is the Database in MongoDB?
The MongoDB database is similar to the RDBMS databases and used to store one or more Collections of JSON-like Documents. MongoDB database can be created with the use database_name command. If the database is already present then that database is selected and if the database is not present then it is created.
8. What is Collection in MongoDB?
MongoDB Collections are used to store the JSON-like Documents. Collections are similar to the table in an RDBMS. Collections are created by using db.createCollection() method in MongoDB. To change the structure of Collection like adding a new field or modifying existing fields a collection has to be restructured.
9. What is Document in MongoDB?
MongoDB Documents are the collection of BSON records, similar to the Rows/Columns in the traditional RDBMS system. The BSON Documents are the binary form JSON documents that have more data type compared with JSON. The MongoDB Document can have multiple files and the data type of each field can be different. The Document can have an _id key which is similar to the primary key in RDBMS.
10. What is CRUD operation in MongoDB?
In MongoDB, the CRUD stands for CREATE, READ, UPDATE and DELETE operations that can be used to perform various operations on the datasets.
- CREATE:- The MongoDB CREATE operation is used to create a new Document in the Collection.
- READ:- The MongoDB READ operation is used to fetch the records from the Collection.
- UPDATE:- The MongoDB UPDATE operation is used to modify the existing Documents in the Collection.
- DELETE:- The MongoDB DELETE operation is used to delete the existing Documents from the Collection.
11. What is the Primary node in MongoDB and what it does?
MongoDB Primary Node is the part of the replica set which receives the read and write operation from the client and processes it. The Primary Node maintains the oplog in which it logs all the operations that further read by secondary nodes to maintain the replication.
12. What are the Secondary nodes in MongoDB?
The MongoDB Secondary Nodes are used to maintain the copy of the Primary Node. It reads the primary's oplog and applies all changes in its datasets. In a Replica set, there could be one or more Secondary Nodes. Since the Secondary Nodes use the primary's oplog to make changes and hence a Client won't be able to perform any write operation on Secondary Nodes but the Client can read data.
13. Does SQL language supported by MongoDB?
MongoDB does not support the SQL language directly however MongoDB provides the MongoDB Connector to connect with the Business Intelligence applications. The MongoDB Connector receives the SQL query from the BI platform and converts it into the MongoDB queries and sends it to MongoDB for execution, once the execution is completed, the MongoDB Connector converts the resultset into the tabular format and send it back to the BI applications.
14. What is a NoSQL database?
The NoSQL databases are termed as non SQL or not only SQL which are used to store data that is not similar to relational tables. NoSQL database stores the relational data as well just like the relation databases store but the difference is that just like a relational database in which the tables are split into multiple tables to maintain the relation, in the NoSQL database the data is stored as a nested form in a single data structure.
15. Explain the types of NoSQL databases?
There are four major types of NoSQL databases which are mentioned below.
1. Document Databases
These databases are used to store the data in the form of JSON (JavaScript Object Notation) objects. MongoDB is the world’s most popular NoSQL Document database.
2. Key-value Databases
In these databases, the data is stored in a key and value form. The value can be fetched by referring to its key. The example of key and value databases are Redis and DynamoDB.
3. Wide-column stores Databases
The Wide-column store's databases are used to store the data in the tables, rows, and dynamic columns. The examples of Wide-column store's databases are Cassandra and HBase.
4. Graph Databases
The Graph databases are used to store data in the form of nodes and edges. The nodes are used to store the detail about the people, pleases, things whereas the edges are used to store the information about the relationship of edges. The example of Graph databases is Neo4j and JanusGraph.
16. What are the differences between SQL and NoSQL?
The following are some of the core differences between SQL and NoSQL databases.
Parameters | SQL Databases | NoSQL Databases |
---|---|---|
Data Storage Model | SQL database uses the Table format to store data. | NoSQL uses the different types of storage models to store data such as JSON Documents format, Wide-column format, Graph's nodes, and edges format, tables format that has rows but the dynamic columns. |
History | SQL databases were developed during the 1970s mainly focus on duplicate data reduction. | NoSQL databases were developed during the 2000s to adopt rapid data change and scalability. |
Examples of databases | The example of SQL databases is Oracle, Microsoft SQL Server, PostgreSQL, and MySQL. | The example of NoSQL databases are CouchDB, MongoDB support for Document model, DynamoDB, Redis supports the key-value data model, HBase, Cassandra supports the Wide-column data model, Amazon Neptune, Neo4j supports the Graph data model. |
Main Purpose | SQL databases are used for general purposes. | NoSQL databases are used for different purposes, such as Document databases are used general-purpose, the key-value databases are used for simple query lookup on the huge data, the wide column database is used for predictable queries, and the graph database is used to analyze the detail of connected data. |
Schemas | Rigid | Flexible |
Database Scaling | SQL databases are Vertically scalable with high-cost large servers. | NoSQL databases are Horizontally scalable with commodity servers. |
Multi-Record ACID Transactions | ACID rules are supported in the SQL databases. | Most of the NoSQL databases do not support the ACID, however, MongoDB does. |
Joins Support | Joins are used in SQL databases and it is required. | Join are not mandatory in NoSQL databases. |
17. How to create a database and collection in MongoDB?
In MongoDB, if a database is not present and if we are going to store any data in that database then it is created similarly a collection is also created if we store any data in the collection. Apart from this, we can manually create the database using the use databasename command and the insert operation can create a new Collection.
use my_newdatabasedb.collectionname.insertOne( { a: 10 } )
18. What are the important benefits of NoSQL databases?
The following are some of the important benefits of NoSQL databases.
- NoSQL databases support Flexible data models that help to make changes in the database.
- NoSQL databases can scale Horizontal by adding commodity hardware based on the load and requirement.
- In NoSQL, the queries execution is fast because the data which are processed together are stored together unlike RDBMS in which relational data is stored in multiple tables and by performing the join operation data can be fetched but when the table data grows the join operation become resource consuming.
- NoSQL databases are easy for developers because they need to write less code and the development becomes faster.
19. How to create MongoDB index and how to view index?
We can create a MongoDB index using the db.collection_name.createIndex() method and to view the index we can use the db.collection_name.getIndexes() method.
20. What is the _id field and what is its behavior in MongoDB?
MongoDB _id field is the unique field that is created for each document in a collection. It is similar to the primary key of a table in RDBMS. If a user omits this field during the insert operation then the MongoDB driver creates it with ObjectId name. The _id field is the first field in Documents.
21. What is the use of replication in MongoDB?
MongoDB Replication is used to create multiple copies of data over multiple nodes to provide high availability and data redundancy. The Replication is useful in cases like Hardware errors, service interruptions. MongoDB achieves the Replication by using the replica set in which the write operation is sent to the primary node and that applies the operation on the secondary node by replicating the data.
22. What is the use of Sharding in MongoDB?
MongoDB sharding is a process of distributing the big dataset over multiple nodes that enables horizontal scaling to handle the intense workloads.
The MongoDB Sharding Cluster consists of the following components.
- shards: Shards are the subset of a Collection also known as chunks.
- routers: The routers also called the mongos, are used to route the application queries on different shards to perform the read and write operation.
- config server replica sets: The config servers are used to maintain the metadata information of the sharded cluster. The metadata information contains the state and organization of all datasets.
23. What are Storage Engines in MongoDB?
MongoDB Storage Engines is used to decide, the mechanism to store data on memory and the disk. MongoDB provides different types of Storage Engines that can be used in various use cases and different types of workload.
The below are the types of MongoDB Storage Engine.
1. MongoDB WiredTiger Storage Engine
This is the default MongoDB Storage Engine that stores data on the disk and is suggested for the new deployments. It also provides compression, checkpointing, and document-level concurrency.
2. MongoDB In-Memory Storage Engine
The In-Memory Storage Engine stores data in memory despite disk and provides better performance compared to Disk storage. This storage engine is available in the MongoDB Enterprise.
24. Why the Documents does not distribute on the Shards?
The distribution of data over multiple shards starts when the size of the chunks reaches a threshold. After reaching the threshold, the MongoDB Balancer starts the balancing of chunks over multiple shards. For example, if the migration threshold is crossing 2 and the number of chunks is less than 20 then the Balancer will start.
25. How to remove a document from the Collection?
We can use the remove() method to remove the Document from the Collection.
26. What is the detail an ObjectId contains?
The MongoDB ObjectId contains the following details.
- The Client machine ID
- The Client process ID
- Byte incremented counter
- Timestamp
27. What is the list of aggregate functions used in MongoDB?
The following is the list of aggregate functions used in MongoDB.
- AVG
- Sum
- Min
- Max
- First
- Push
- addTo Set
- Last
28. What are the data types in MongoDB?
The following is the list of data types in MongoDB.
- Integer
- String
- Boolean
- Array
- Double
- Date
- Timestamp
- Regular Expression
29. What is the BSON?
BSON is the binary-encoded format of JSON that provides more data types compared to JSON.
- array
- binData
- bool
- date
- dbPointer
- decimal
- double
- int
- javascript
- javascriptWithScope
- long
- maxKey
- minKey
- null
- object
- objectId
- regex
- string
- symbol
- timestamp
- undefined
30. Explain the different types of indexes in MongoDB?
The following is the list of different types of Indexes in MongoDB.
- Default: It is by default created by MongoDB with the name "_id".
- Compound: It is created in multiple fields.
- Multi-key: Useful for array data.
- Single field: It is created on a single field by sorting.
- Geospatial: It is created for location data.
- Hashed: The Indexing is done on the hashes of the multiple fields.