
MongoDB is a NoSQL database
NoSQL stands for "Not Only SQL". NoSQL is a type of database that can handle and sort all type of unstructured, semi-structured and messy and complicated big data.
MongoDB is written in C++.
A collection in MongoDB is a group of documents. A collection in MongoDB is very similar to table in a RDBMS.
A capped collections are fixed-size collections that support high-throughput operations that insert and retrieve documents based on insertion order. A capped collections work in a way similar to circular buffers: once a collection fills its allocated space, it makes room for new documents by overwriting or deleting the oldest documents in the collection automatically.
Following are the use of cappped collection
- Store log information generated by high-volume systems
- Cache small amounts of data in a capped collections
The above command will create a capped collection - log
-
High Performance -MongoDB provides high performance data persistence. It supports embedded data models to reduce I/O activity on a database system, as well as indexes for faster queries, and can include keys from embedded documents and arrays
-
High Availability -To provide high quality availability, MongoDb’s replication facility (known as replica sets) provide both automatic failover and data redundancy. A replica set is a group of MongoDB servers that maintain the same data set and provide both redundancy and increased data availability
-
Automatic Scaling -MongoDB provides horizontal scalability as part of its core functionality. Automatic sharding distributes data across a cluster of machines, while replica sets can provide eventually-consistent reads for low-latency deployments
-
Schema Less -JSON data model with dynamic schemas
-
Semi-Structured/Unstructured -Best Suited for Semi-Structured/Unstructured Data as well as Structured data also Ad hoc queries, indexing, and real time aggregation provide powerful ways to access and analyze your data
You may also like - React.js Interview Questions
- Key-Value database
- Document based database
- Column based database
- Graph base database
-
Key-Value database:
Every item in the database is stored as an attribute name (or "key") together with its value.
#Example: DynamoDB (Amazon S3) and Redis (In Memory Database), etc.
-
Document based database:
It stores data in form of documents (BSON format) and it can contains many different key-value pairs, or key-array pairs, or even nested documents.
#Example: MongoDB
-
Column based database:
It stores data together as columns instead of rows and are optimized for queries over large datasets.
#Example: Cassandra, HBase, etc
- Graph based database
It is used to store information about networks, such as social connections.
#Example: Neo4J and HyperGraphDB, etc
MongoDB provides official driver support for NodeJS, PHP, C/C++, C#, Java, Python, etc.
mongod is MongoDB server. it start the MongoDB process and run it in the background.
The mongos tracks what data is on which shard by caching the metadata from the config servers.
mongo is the command-line shell that connects to a specific instance of mongod.
MongoDB projection means selecting only the necessary data rather than selecting whole of the data of a document.
Yes, but it have the dynamic and flexible schema so there is no need to define structure to create collections in MongoDB.
No. by default, MongoDB doesn't support primary key-foreign key relationship.
but we can achieve something similar like primary key-foreign key relationship using following methods:
- Embedding - A document nested inside another document.
- Referencing - It store the _id of an document into another document to refer.
The namespace is a combination of the database name and the name of the collection or index.
Example: [database-name].[collection-or-index-name]
oplog stands for operations log is a special capped collection which stores an ordered history of all logical operations that modify the data stored in your database.
You may also like - JavaScript Interview Questions
Aggregation operations process data records and return computed results.
Aggregation operations group values from multiple documents together, and can perform a variety of operations on the grouped data to return a single result.
MongoDB provides three ways to perform aggregation:
- aggregation pipeline
- map-reduce function.
- single purpose aggregation methods.
Aggregation Pipeline - A series of operations in an aggregation process.
The MongoDB aggregation pipeline consists of stages.
- Each and evenry stage transforms the documents as they pass through the pipeline.
- Pipeline stages do not need to produce output documents for each and every input documents.
- MongoDB provides the db.collection.aggregate() method.
db.collection.aggregate([ {$project:{}} {$match: {}}, {$group: {}}, {$sort: {}} ])
Note- Pipeline stages can appear multiple times in the pipeline.
There are following possible stages in aggregation pipeline.
$project − It is used to select only specific fields from a collection.
$match − It is used to filter out the document based on some conditions.
$group − It is used to group the documents.
$sort − It is used to sorts the documents.
$skip − It is used to skip documents.
$limit − It is used to limit the documents.
$unwind − It is used to unwind documents.
ObjectId is a 12-byte BSON type.
ObjectId consists of
- 4 bytes represents seconds
- 3 bytes machine identifier
- 2 bytes process id
- 3 bytes counter
In MongoDB, Indexes are used to execute query efficiently.
It imporoves the search performance but the same time if you will create too many unnecessary index then it may slow down write operations.
By default, the _id index is created for every collection in MongoDB.
To create an index in the Mongo Shell, use db.collection.createIndex().
#Example:
There are following types of Indexes
- Single Index
- Compound Index
- Multikey Index
- Geospatial Index
- Text Index
- Hashed Index
The following example creates a compound index on the username field (in ascending order) and the city field (in descending order.)
The order of an index is important for supporting sort() operations using the index.
- To add or change index options you must drop the index using the dropIndex() method and issue another createIndex() operation with the new options.
- If you create an index with one set of options, and then issue the createIndex() method with the same index fields and different options without first dropping the index, createIndex() will not rebuild the existing index with the new options.
- If you call multiple createIndex() methods with the same index specification at the same time, only the first operation will succeed, all other operations will have no effect.
- Non-background indexing operations will block all other operations on a database.
- MongoDB will not create an index on a collection if the index entry for an existing document exceeds the Maximum Index Key Length.
- Use db.collection.createIndex() rather than db.collection.ensureIndex() to create indexes.
- Use db.collection.getIndexes() to view the specifications of existing indexes for a collection.
For more details - please visit MongoDB Official Website
MonogDB doesn't supports transaction
- MongoDB doesn't use traditional locking or complex transaction with Commit and Rollback.
- MongoDB is designed to be light weighted, fast and predictable to its performance. It keeps transaction support simple to enhance performance.
You may also like - MySQL Interview Questions
A storage engine is the part of a database that is responsible for managing how data is stored on disk.
#Example, one storage engine might offer better performance for read-heavy workloads, and another might support a higher-throughput for write operations.
You can find the storage engine currently being used.
Once it is confirmed that wiredTiger is being used then type
to get all the configuration details of wiredTiger.
- MMAPv1
- WiredTiger
A tool that, when enabled, keeps a record on all long-running operations in a database's system.profile collection.
The profiler is most often used to diagnose slow queries.
Enable profiling:
Now let it run for a while. It collects the slow queries ( > 100ms) into a capped collections, so queries go in and if it's full, old queries go out, so don't be surprised that it's a moving target
Find the most recent slow query:
A data processing and aggregation paradigm consisting of a "map" phase that selects data and a "reduce" phase that transforms the data. In MongoDB, you can run arbitrary aggregations over data using map-reduce.
For map-reduce operations, MongoDB provides the mapReduce database command.
- Replication is the process of synchronizing data across multiple servers.
- Replication provides redundancy and increases data availability with multiple copies of data on different database servers
- Replication also allows you to recover from hardware failure and service interruptions.
A database architecture that partitions data by key ranges and distributes the data among two or more database instances. Sharding enables horizontal scaling.
- In MongoDB, Sharding is a procedure of storing data records across multiple machines.
- It is a MongoDB approach to meet the demands of data growth.
- It creates horizontal partition of data in a database or search engine.
- Each partition is referred as shard or database shard.
MongoDB Sharding is range-based.
So all the objects in a collection lies into chunks. Only when there is more than 1 chunk there is an option for multiple Shards to get data. Right now, the default chunk size is 64mb, so you need at least 64mb for migration.
If a Shard is down, the query will return an error unless the 'Partial' query options is set.
If a shard is responding slowly, Mongos will wait for it.
//Output
{"current" : CURRENT_CONNECTION_COUNT, "available" : TOTAL_CONNECTION_COUNTS}
//sample output
{"current" : 15, "available" : 15000}
- Document Size: Max document size is 16 MB.
-
Transactions: There is no default transaction support.
RDBMS is best choice for transactions based applications - Joins: No Support for Joins as in RDBMS.