Failed Write Operations to Be Processes Again in Flockdb
MongoDB Interview Questions ( v4.4 )
Click if you like the project. Pull Request are highly appreciated.
Table of Contents
- MongoDB Commands
- MongoDB Coding Practice
Q. What are NoSQL databases? What are the dissimilar types of NoSQL databases?
NoSQL is a not-relational DBMS, that does not require a fixed schema, avoids joins, and is easy to scale. The purpose of using a NoSQL database is for distributed data stores with humongous information storage needs. NoSQL is used for Large data and existent-fourth dimension web apps.
Types of NoSQL Databases
- Document databases
- Cardinal-value stores
- Column-oriented databases
- Graph databases
one. Document databases
A document database stores information in JSON, BSON , or XML documents. In a document database, documents tin be nested. Detail elements can be indexed for faster querying.
Documents tin exist stored and retrieved in a course that is much closer to the data objects used in applications, which means less translation is required to utilise the data in an application. SQL information must often exist assembled and disassembled when moving dorsum and forth between applications and storage.
Case: Amazon SimpleDB, CouchDB, MongoDB, Riak, Lotus Notes are popular Certificate originated DBMS systems.
2. Primal-value Stores
Data is stored in key/value pairs. Information technology is designed in such a way to handle lots of data and heavy load. Central-value pair storage databases store information as a hash table where each primal is unique, and the value can be a JSON, BLOB(Binary Big Objects), cord, etc.
Case: of primal-value stores are Redis, Voldemort, Riak, and Amazon's DynamoDB.
3. Column-Oriented Databases
Column-oriented databases work on columns and are based on BigTable paper by Google. Every column is treated separately. The values of single cavalcade databases are stored contiguously.
They deliver high performance on assemblage queries like SUM, COUNT, AVG, MIN, etc. as the information is readily bachelor in a column.
Case: Column-based NoSQL databases are widely used to manage information warehouses, business intelligence, CRM, Library carte du jour catalogs, HBase, Cassandra, HBase, Hypertable are examples of a column-based database.
4. Graph Databases
A graph type database stores entities as well the relations amongst those entities. The entity is stored as a node with the relationship every bit edges. An edge gives a relationship betwixt nodes. Every node and edge has a unique identifier.
Compared to a relational database where tables are loosely connected, a Graph database is a multi-relational in nature. Traversing relationships as fast as they are already captured into the DB, and there is no need to calculate them.
Graph base of operations databases mostly used for social networks, logistics, spatial data.
Example: Neo4J, Space Graph, OrientDB, FlockDB are some popular graph-based databases.
Q. What is MongoDB?
MongoDB is a document-oriented NoSQL database used for high volume data storage. Instead of using tables and rows equally in the traditional relational databases, MongoDB makes use of collections and documents. Documents consist of key-value pairs which are the basic unit of data in MongoDB. Collections contain sets of documents and function which is the equivalent of relational database tables.
Fundamental Features
- Document Oriented and NoSQL database.
- Supports Aggregation
- Uses BSON format
- Sharding (Helps in Horizontal Scalability)
- Supports Ad Hoc Queries
- Schema Less
- Capped Collection
- Indexing (Any field in MongoDB can be indexed)
- MongoDB Replica Set (Provides high availability)
- Supports Multiple Storage Engines
Key Components
i. _id: The _id
field represents a unique value in the MongoDB certificate. The _id
field is like the certificate'south chief key. If yous create a new certificate without an _id
field, MongoDB volition automatically create the field.
2. Collection: This is a grouping of MongoDB documents. A drove is the equivalent of a tabular array which is created in any other RDMS such every bit Oracle.
three. Cursor: This is a arrow to the issue set up of a query. Clients can iterate through a cursor to retrieve results.
4. Database: This is a container for collections similar in RDMS wherein information technology is a container for tables. Each database gets its own fix of files on the file system. A MongoDB server can shop multiple databases.
5. Certificate: A record in a MongoDB collection is basically chosen a document. The document, in turn, will consist of field name and values.
6. Field: A name-value pair in a document. A document has goose egg or more than fields. Fields are analogous to columns in relational databases.
Example:
Connecting MongoDB Cloud using MongoDB Compass
[Read More]
Q. What are Indexes in MongoDB?
Indexes support the efficient execution of queries in MongoDB. Without indexes, MongoDB must perform a collection scan, i.eastward. browse every document in a collection, to select those documents that match the query statement. If an appropriate index exists for a query, MongoDB can employ the alphabetize to limit the number of documents information technology must inspect.
Indexes are special information structures that store a modest portion of the collection'south data prepare in an easy to traverse form. The index stores the value of a specific field or set up of fields, ordered by the value of the field. The ordering of the index entries supports efficient equality matches and range-based query operations. In addition, MongoDB can render sorted results by using the ordering in the index.
Instance
The createIndex()
method merely creates an index if an alphabetize of the same specification does non already exist. The following example ( using Node.js ) creates a single key descending index on the name field:
collection . createIndex ( { proper name : - 1 }, office ( err , issue ) { console . log ( result ); callback ( effect ); }
Q. What are the types of Indexes available in MongoDB?
MongoDB supports the following types of the index for running a query.
1. Single Field Index
MongoDB supports user-divers indexes similar single field alphabetize. A single field alphabetize is used to create an alphabetize on the single field of a document. With single field index, MongoDB can traverse in ascending and descending society. By default, each collection has a single field alphabetize automatically created on the _id
field, the primary central.
Example
{ " _id " : 1 , " person " : { name : " Alex " , surname : " K " }, " historic period " : 29 , " city " : " New York " }
Nosotros tin can ascertain, a single field index on the age field.
db . people . createIndex ( { age : 1 } ) // creates an ascending index db . people . createIndex ( { age : - one } ) // creates a descending index
With this kind of index we can improve all the queries that observe documents with a status and the age field, like the following:
db . people . find ( { age : xx } ) db . people . find ( { name : " Alex " , age : 30 } ) db . people . find ( { age : { $gt : 25 } } )
ii. Compound Index
A compound index is an alphabetize on multiple fields. Using the aforementioned people collection we can create a compound index combining the urban center and historic period field.
db . people . createIndex ( { metropolis : 1 , age : 1 , person . surname : 1 } )
In this example, we have created a compound index where the starting time entry is the value of the city field, the second is the value of the historic period field, and the 3rd is the person.proper noun. All the fields here are defined in ascending order.
Queries such as the following can benefit from the index:
db . people . detect ( { city : " Miami " , historic period : { $gt : fifty } } ) db . people . find ( { metropolis : " Boston " } ) db . people . find ( { city : " Atlanta " , age : { $lt : 25 }, " person.surname " : " Green " } )
3. Multikey Index
This is the index type for arrays. When creating an alphabetize on an array, MongoDB volition create an index entry for every chemical element.
Case
{ " _id " : one , " person " : { name : " John " , surname : " Brownish " }, " age " : 34 , " city " : " New York " , " hobbies " : [ " music " , " gardening " , " skiing " ] }
The multikey index can exist created as:
db . people . createIndex ( { hobbies : ane } )
Queries such as these side by side examples volition use the alphabetize:
db . people . find ( { hobbies : " music " } ) db . people . find ( { hobbies : " music " , hobbies : " gardening " } )
4. Geospatial Alphabetize
GeoIndexes are a special index type that allows a search based on location, distance from a point and many other different features. To query geospatial data, MongoDB supports two types of indexes – 2d indexes
and 2d sphere indexes
. 2nd indexes use planar geometry when returning results and 2dsphere indexes apply spherical geometry to return results.
five. Text Index
It is another blazon of index that is supported by MongoDB. Text index supports searching for string content in a drove. These index types practice not store language-specific terminate words (e.k. "the", "a", "or"). Text indexes restrict the words in a drove to only store root words.
Example
Permit'due south insert some sample documents.
var entries = db . people ( " blogs " ). entries ; entries . insert ( { title : " my blog mail " , text : " i am writing a blog. yay " , site : " home " , language : " english " }); entries . insert ( { title : " my second post " , text : " this is a new blog i am typing. yay " , site : " piece of work " , language : " english " }); entries . insert ( { championship : " knives are Fun " , text : " this is a new blog i am writing. yay " , site : " home " , language : " english " });
Allow'southward define create the text index.
var entries = db . people ( " blogs " ). entries ; entries . ensureIndex ({ title : " text " , text : " text " }, { weights : { championship : 10 , text : 5 }, name : " TextIndex " , default_language : " english " , language_override : " language " });
Queries such equally these next examples will use the index:
var entries = db . people ( " blogs " ). entries ; entries . find ({ $text : { $search : " blog " }, site : " home " })
vi. Hashed Index
MongoDB supports hash-based sharding and provides hashed indexes. These indexes are the hashes of the field value. Shards use hashed indexes and create a hash according to the field value to spread the writes across the sharded instances.
Q. Explain Index Backdrop in MongoDB?
one. TTL Indexes
TTL ( Time To Live ) is a special choice that we can apply only to a unmarried field alphabetize to let the automatic deletion of documents after a certain time.
During index creation, we can ascertain an expiration time. Afterwards that fourth dimension, all the documents that are older than the expiration time will be removed from the collection. This kind of feature is very useful when nosotros are dealing with data that don't demand to persist in the database ( eg. session data
).
Instance
db . sessionlog . createIndex ( { " lastUpdateTime " : 1 }, { expireAfterSeconds : 1800 } )
In this case, MongoDB will driblet the documents from the collection automatically once half an hour (1800 seconds) has passed since the value in lastUpdateTime field.
Restrictions
- Only unmarried field indexes can take the TTL pick
- the
_id
single field index cannot support the TTL option - the indexed field must be a date blazon
- a capped collection cannot have a TTL index
two. Partial indexes
A partial alphabetize is an index that contains only a subset of the values based on a filter rule. They are useful in cases where:
- The index size can be reduced
- We want to index the most relevant and used values in the query conditions
- We want to index the most selective values of a field
Case
db . people . createIndex ( { " city " : 1 , " person.surname " : 1 }, { partialFilterExpression : { age : { $lt : 30 } } } )
We have created a chemical compound index on city and person.surname but simply for the documents with age less than 30. In order for the partial index to be used the queries must comprise a condition on the historic period field.
db . people . observe ( { urban center : " New Tork " , age : { $eq : 20 } } )
3. Sparse indexes
Sparse indexes are a subset of partial indexes. A sparse index only contains elements for the documents that accept the indexed field, even if it is null.
Since MongoDB is a schemaless database, the documents in a drove can accept different fields, and so an indexed field may non be present in some of them.
Example
To create such an index utilise the sparse option:
db . people . createIndex ( { city : i }, { sparse : true } )
In this example, we are assuming there could be documents in the collection with the field metropolis missing. Sparse indexes are based on the existence of a field in the documents and are useful to reduce the size of the index.
four. Unique indexes
MongoDB can create an index equally unique. An index defined this fashion cannot incorporate duplicate entries.
Instance
db . people . createIndex ( { metropolis : 1 }, { unique : true } )
Uniqueness can be divers for chemical compound indexes also.
db . people . createIndex ( { city : ane , person . surname : ane }, { unique : true } )
By default, the index on _id
is automatically created as unique.
Q. How many indexes does MongoDB create by default for a new collection?
By default MongoDB creates a unique index on the _id
field during the cosmos of a collection. The _id
index prevents clients from inserting two documents with the same value for the _id
field.
Q. Can you lot create an index in an array field in MongoDB?
Yep, To alphabetize a field that holds an array value, MongoDB creates an alphabetize key for each element in the array. Multikey indexes can be constructed over arrays that hold both scalar values (e.1000. strings, numbers) and nested documents. MongoDB automatically creates a multikey index if any indexed field is an array.
Syntax
db . collection . createIndex ( { < field > : < 1 or - 1 > } )
For example, consider an inventory collection that contains the post-obit documents:
{ _id : 10 , blazon : " nutrient " , particular : " aaa " , ratings : [ five , eight , 9 ] } { _id : 11 , type : " food " , item : " bbb " , ratings : [ 5 , 9 ] } { _id : 12 , type : " food " , detail : " ccc " , ratings : [ 9 , 5 , viii , 4 , 7 ] }
The collection has a multikey index on the ratings field:
db . inventory . createIndex ( { ratings : one } )
The post-obit query looks for documents where the ratings field is the array [ 5, 9 ]:
db . inventory . find ( { ratings : [ five , ix ] } )
MongoDB tin can use the multikey index to discover documents that have 5 at any position in the ratings array. So, MongoDB retrieves these documents and filters for documents whose ratings assortment equals the query array [ 5, 9 ].
Q. Why does Profiler use in MongoDB?
The database profiler captures data data about read and write operations, cursor operations, and database commands. The database profiler writes data in the arrangement.profile
collection, which is a capped drove.
The database profiler collects detailed information about Database Commands executed against a running mongod example. This includes Crud operations equally well as configuration and administration commands.
Profiler has three profiling levels.
- Level 0 - Profiler will not log any data
- Level 1 - Profiler volition log only slow operations higher up some threshold
- Level ii - Profiler will log all the operations
1. To go electric current profiling level.
db.getProfilingLevel() // Output 0
2. To check current profiling status
db.getProfilingStatus() // Output { "was" : 0, "slowms" : 100 }
three. To fix profiling level
db.setProfilingLevel(ane, 40) // Output { "was" : 0, "slowms" : 100, "ok" : 1 }
Q. How to remove attribute from MongoDB Object?
$unset
The $unset
operator deletes a particular field. If the field does non exist, so $unset
does zero. When used with $
to lucifer an assortment chemical element, $unset
replaces the matching element with goose egg
rather than removing the matching element from the array. This behavior keeps consistent the array size and element positions.
syntax:
{ $unset : { < field1 > : "" , ... } }
Instance:
delete the backdrop.service
attribute from all records on this collection.
db . collection . update ( {}, { $unset : { " properties.service " : i } }, { multi : truthful } );
To verify they accept been deleted you tin utilise:
db . collection . find ( { " properties.service " : { $exists : truthful } } ). count ( true );
Q. What is "Namespace" in MongoDB?
MongoDB stores BSON (Binary Interchange and Structure Object Notation) objects in the drove. The concatenation of the collection name and database name is called a namespace
Q. What is Replication in Mongodb?
Replication exists primarily to offer data redundancy and loftier availability. Information technology maintain the immovability of data past keeping multiple copies or replicas of that information on physically isolated servers. Replication allows to increase information availability past creating multiple copies of data across servers. This is especially useful if a server crashes or hardware failure.
With MongoDB, replication is achieved through a Replica Set. Writer operations are sent to the primary server (node), which applies the operations across secondary servers, replicating the information. If the principal server fails (through a crash or organisation failure), one of the secondary servers takes over and becomes the new primary node via election. If that server comes back online, it becomes a secondary once it fully recovers, aiding the new main node.
Q. What is Replica Set in MongoDB?
It is a group of mongo processes that maintain same data prepare. Replica sets provide back-up and high availability, and are the footing for all production deployments. A replica fix contains a primary node and multiple secondary nodes.
The primary node receives all write operations. A replica fix can take simply one primary capable of confirming writes with { w: "majority" }
write concern; although in some circumstances, another mongod instance may transiently believe itself to also be primary.
The secondaries replicate the primary's oplog and apply the operations to their data sets such that the secondaries' data sets reflect the primary'south data fix. If the principal is unavailable, an eligible secondary will agree an election to elect itself the new principal.
Q. How does MongoDB ensure high availability?
High Availability (HA) refers to the improvement of organisation and app availability by minimizing the downtime caused past routine maintenance operations (planned) and sudden system crashes (unplanned).
Replica Ready
The replica set mechanism of MongoDB has ii principal purposes:
- Ane is for data redundancy for failure recovery. When the hardware fails, or the node is down for other reasons, yous can utilise a replica for recovery.
- The other purpose is for read-write splitting. Information technology routes the reading requests to the replica to reduce the reading force per unit area on the main node.
MongoDB automatically maintains replica sets, multiple copies of data that are distributed across servers, racks and data centers. Replica sets help preclude database downtime using native replication and automatic failover.
A replica set consists of multiple replica set up members. At any given time, one member acts as the primary member, and the other members act as secondary members. If the main fellow member fails for any reason (e.m., hardware failure), 1 of the secondary members is automatically elected to primary and begins to procedure all reads and writes.
Q. What is an Embedded MongoDB Certificate?
An embedded, or nested, MongoDB Document is a normal document that is nested inside some other document inside a MongoDB drove. Embedding connected data in a single certificate can reduce the number of read operations required to obtain data. In general, we should structure our schema so that awarding receives all of its required data in a single read functioning.
Example:
In the normalized data model, the address documents comprise a reference to the patron document.
// patron document { _id : " joe " , name : " Joe Bookreader " } // accost documents { patron_id : " joe " , // reference to patron certificate street : " 123 Fake Street " , city : " Faketon " , land : " MA " , zip : " 12345 " } { patron_id : " joe " , street : " 1 Some other Street " , urban center : " Boston " , state : " MA " , zip : " 12345 " }
Embedded documents are especially useful when a one-to-many human relationship exists between documents. In the example shown in a higher place, we encounter that a single customer has multiple addresses associated with him. The nested document construction makes it easy to retrieve consummate address data virtually this customer with just a single query.
Q. How can you lot achieve primary central - foreign primal relationships in MongoDB?
The primary key-foreign central relationship tin be achieved by embedding one document inside the some other. As an example, a section document can accept its employee document(due south).
Q. When should we embed one document within some other in MongoDB?
Y'all should consider embedding documents for:
- contains relationships betwixt entities
- 1-to-many relationships
- Functioning reasons
Q. How is data stored in MongoDB?
In MongoDB, Data is stored in BSON documents (curt for Binary JSON
). These documents are stored in MongoDB in JSON (JavaScript Object Annotation) format. JSON documents support embedded fields, so related data and lists of data tin exist stored with the certificate instead of an external table. Documents incorporate one or more fields, and each field contains a value of a specific data blazon, including arrays, binary information and sub-documents. Documents that tend to share a similar structure are organized as collections.
JSON is formatted as proper name/value pairs. In JSON documents, field names and values are separated by a colon, field name and value pairs are separated past commas, and sets of fields are encapsulated in "curly braces" ({}).
Case:
{ " proper noun " : " notebook " , " qty " : 50 , " rating " : [ { " score " : 8 }, { " score " : 9 } ], " size " : { " height " : eleven , " width " : 8.5 , " unit " : " in " }, " status " : " A " , " tags " : [ " college-ruled " , " perforated " ] }
Q. What are the differences between MongoDB and SQL-SERVER?
- The MongoDB shop the data in documents with JSON format but SQL store the data in Tabular array format.
- The MongoDB provides high performance, high availability, like shooting fish in a barrel scalability etc. rather than SQL Server.
- In the MongoDB, we can modify the construction merely by adding, removing column from the existing documents.
MongoDB and SQL Server Comparision Tabular array
Base of operations of Comparison | MS SQL Server | MongoDB |
---|---|---|
Storage Model | RDBMS | Certificate-Oriented |
Joins | Yeah | No |
Transaction | ACID | Multi-document ACID Transactions with snapshot isolation |
Agile practices | No | Yes |
Data Schema | Fixed | Dynamic |
Scalability | Vertical | Horizontal |
Map Reduce | No | Yep |
Language | SQL query language | JSON Query Linguistic communication |
Secondary alphabetize | Yes | Yeah |
Triggers | Yes | Yeah |
Foreign Keys | Aye | No |
Concurrency | Yes | yes |
XML Support | Yes | No |
Q. How can you lot achieve transaction and locking in MongoDB?
In MongoDB (four.2), an operation on a single document is atomic. For situations that require atomicity of reads and writes to multiple documents (in a single or multiple collections), MongoDB supports multi-document transactions. With distributed transactions, transactions tin can be used beyond multiple operations, collections, databases, documents, and shards.
MongoDB allows multiple clients to read and write the same data. In order to ensure consistency, it uses locking and other concurrency control measures to prevent multiple clients from modifying the same piece of data simultaneously.
MongoDB uses multi-granularity locking that allows operations to lock at the global, database or collection level, and allows for individual storage engines to implement their ain concurrency command below the collection level (e.g., at the certificate-level in WiredTiger). MongoDB uses reader-writer locks that allow concurrent readers shared access to a resources, such as a database or collection.
The lock modes are represented as follows:
Lock Mode | Description |
---|---|
R | Represents Shared (Due south) lock. |
Westward | Represents Exclusive (X) lock. |
r | Represents Intent Shared (IS) lock. |
west | Represents Intent Exclusive (9) lock. |
Example:
The following example highlights the key components of the transactions API
const client = new MongoClient ( uri ); wait customer . connect (); // Prereq: Create collections. await client . db ( ' mydb1 ' ). drove ( ' foo ' ). insertOne ({ abc : 0 }, { west : ' majority ' }); await client . db ( ' mydb2 ' ). collection ( ' bar ' ). insertOne ({ xyz : 0 }, { w : ' majority ' }); // Step 1: Starting time a Customer Session const session = client . startSession (); // Step 2: Optional. Define options to employ for the transaction const transactionOptions = { readPreference : ' primary ' , readConcern : { level : ' local ' }, writeConcern : { w : ' majority ' } }; // Step three: Use withTransaction to kickoff a transaction, execute the callback, and commit (or arrest on error) // Note: The callback for withTransaction MUST be async and/or return a Promise. try { wait session . withTransaction ( async () => { const coll1 = client . db ( ' mydb1 ' ). drove ( ' foo ' ); const coll2 = client . db ( ' mydb2 ' ). collection ( ' bar ' ); // Important:: You must pass the session to the operations wait coll1 . insertOne ({ abc : 1 }, { session }); look coll2 . insertOne ({ xyz : 999 }, { session }); }, transactionOptions ); } finally { await session . endSession (); await client . close (); }
Q. When to Use MongoDB Rather than MySQL?
ane. MongoDB
MongoDB is one of the most popular document-oriented databases under the banner of NoSQL database. It employs the format of key-value pairs, hither called document shop. Certificate stores in MongoDB are created is stored in BSON files which are, in fact, a little-modified version of JSON files and hence all JS are supported.
Information technology offers greater efficiency and reliability which in turn tin meet your storage capacity and speed demands. The schema-gratuitous implementation of MongoDB eliminates the prerequisites of defining a stock-still structure. These models permit hierarchical relationships representation and facilitate the ability to modify the structure of the record.
Pros
- MongoDB has a lower latency per query & spends less CPU time per query because information technology is doing a lot less work (east.g. no joins, transactions). Equally a consequence, information technology tin can handle a higher load in terms of queries per second.
- MongoDB is easier to shard (apply in a cluster) because it doesn't accept to worry about transactions and consistency.
- MongoDB has a faster write speed considering it does not have to worry about transactions or rollbacks (and thus does not take to worry about locking).
- Information technology supports many Features like automatic repair, easier information distribution, and simpler data models make administration and tuning requirements bottom in NoSQL.
- NoSQL databases are cheap and open up source.
- NoSQL database support caching in arrangement retentivity so it increases data output operation.
Cons
- MongoDB does non support transactions.
- In general, MongoDB creates more work (east.chiliad. more than CPU cost) for the client server. For example, to join data 1 has to outcome multiple queries and do the bring together on the client.
- No Stored Procedures in mongo dB (NoSQL database).
Reasons to Employ a NoSQL Database
- Storing large volumes of information without structure: A NoSQL database doesn't limit storable data types. Plus, you lot can add new types equally business concern needs change.
- Using cloud calculating and storage: Cloud-based storage is a great solution, simply information technology requires information to exist hands spread across multiple servers for scaling. Using affordable hardware on-site for testing and and so for production in the cloud is what NoSQL databases are designed for.
- Rapid development: If you are developing using modern agile methodologies, a relational database will slow y'all down. A NoSQL database doesn't require the level of preparation typically needed for relational databases.
ii. MySQL
MySQL is a popular open up-source relational database management system (RDBMS) that is developed, distributed and supported by Oracle Corporation. MySQL stores data in tables and uses structured query linguistic communication (SQL) for database access. It uses Structured Query Language SQL to access and transfer the data and commands such as 'SELECT', 'UPDATE', 'INSERT' and 'DELETE' to manage it.
Related information is stored in different tables merely the concept of JOIN operations simplifies the process of correlating it and performing queries across multiple tables and minimize the chances of information duplication. It follows the ACID (Diminutive, Consistent, Isolated and Durable) model. This means that in one case a transaction is consummate, the data remains consistent and stable on the disc which may include distinct multiple memory locations.
Pros
- SQL databases are table based databases.
- Data shop in rows and columns
- Each row contains a unique example of data for the categories defined past the columns.
- Provide facility primary key, to uniquely identify the rows.
Cons
- Users have to scale relational database on powerful servers that are expensive and difficult to handle. To scale relational database, it has to be distributed on to multiple servers. Handling tables across unlike servers is difficult.
- In SQL server's data has to fit into tables anyhow. If your information doesn't fit into tables, and so you need to design your database structure that will exist circuitous and once again difficult to handle.
Q. How MongoDB supports ACID transactions and locking functionalities?
ACID stands that any update is:
- Atomic: information technology either fully completes or it does not
- Consistent: no reader will see a "partially applied" update
- Isolated: no reader volition meet a "dirty" read
- Durable: (with the advisable write business organization)
MongoDB, has always supported ACID transactions in a single document and, when leveraging the document model accordingly, many applications don't need ACID guarantees across multiple documents.
MongoDB is a document based NoSQL database with a flexible schema. Transactions are not operations that should be executed for every write operation since they incur a greater performance cost over a unmarried certificate writes. With a document based structure and denormalized data model, at that place will exist a minimized need for transactions. Since MongoDB allows document embedding, you don't necessarily need to use a transaction to meet a write operation.
MongoDB version 4.0 provides multi-document transaction support for replica gear up deployments only and probably the version 4.ii will extend support for sharded deployments.
Instance: Multi-Document ACID Transactions in MongoDB
These are multi-statement operations that need to exist executed sequentially without affecting each other. For instance below nosotros tin can create ii transactions, one to add a user and another to update a user with a field of age.
$session . startTransaction () db . users . insert ({ _id : 6 , name : " John " }) db . users . updateOne ({ _id : iii , { $ready : { age : 26 } }}) session . commit_transaction ()
Transactions can be applied to operations against multiple documents independent in one or many collection/database. Any changes due to document transaction do not impact performance for workloads not related or practice non require them. Until the transaction is committed, uncommitted writes are neither replicated to the secondary nodes nor are they readable outside the transactions.
Q. What are the best practices for MongoDB Transactions?
The multi-certificate transactions are merely supported in the WiredTiger
storage engine. For a single Acrid transaction, if you try performing an excessive number of operations, it can result in loftier pressure level on the WiredTiger cache. The cache is always dictated to maintain country for all subsequent writes since the oldest snapshot was created. This means new writes volition accrue in the cache throughout the duration of the transaction and will be flushed only afterwards transactions currently running on onetime snapshots are committed or aborted.
For the best database performance on the transaction, developers should consider:
-
Always change a small number of documents in a transaction. Otherwise, y'all will need to intermission the transaction into different parts and process the documents in different batches. At most, process yard documents at a time.
-
Temporary exceptions such every bit pending to elect main and transient network hiccups may result in abortion of the transaction. Developers should establish a logic to retry the transaction if the divers errors are presented.
-
Configure optimal duration for the execution of the transaction from the default 60 seconds provided by MongoDB. Besides, use indexing so that information technology can allow fast data access within the transaction.
-
Decompose your transaction into a minor set of performance then that it fits the 16MB size constraints. Otherwise, if the functioning together with oplog clarification exceed this limit, the transaction will exist aborted.
-
All information relating to an entity should exist stored in a single, rich document structure. This is to reduce the number of documents that are to be buried when unlike fields are going to exist changed.
Q. Explain limitations of MongoDB Transactions?
MongoDB transactions can exist simply for relatively short time periods. Past default, a transaction must span no more than 1 minute of clock time. This limitation results from the underlying MongoDB implementation. MongoDB uses MVCC, but unlike databases such equally Oracle, the "older" versions of information are kept only in memory.
- Y'all cannot create or drop a collection inside a transaction.
- Transactions cannot make writes to a capped collection
- Transactions accept plenty of time to execute and somehow they can slow the performance of the database.
- Transaction size is limited to 16MB requiring ane to divide whatever that tends to exceed this size into smaller transactions.
- Subjecting a large number of documents to a transaction may exert excessive pressure on the WiredTiger engine and since it relies on the snapshot adequacy, there volition be a retention of big unflushed operations in retentiveness. This renders some performance cost on the database.
Q. Should I normalize my data earlier storing it in MongoDB?
Data used by multiple documents can either be embedded (denormalized) or referenced (normalized). Normalization, which is increasing the complexity of the schema by splitting tables into multiple smaller ones to reduce the information redundancy( 1NF, 2NF, 3NF).
But Mongo follows the verbal opposite mode of what we do with SQL. In MongoDB, data normalization is not requried. Indeed we need to de-normalize and fit it into a drove of multiple documents.
Example: Let's say we have iii tables
- Table - 1 : ColumnA, ColumnB (main central)
- Tabular array - two : ColumnC (Foreign key), ColumnD (primary key)
- Table - three : ColumnE (foreign fundamental), ColumnF
In this instance, mongoDB certificate construction should be as follows.
{ ColumnA : ValueA , ColumnB : ValueB , Subset1 : [{ ColumnC : ValueC , ColumnD : ValueD , Subset2 : [{ ColumnE : ValueE , ColumnF : ValueF }] }] }
Q. What is upsert operation in MongoDB?
Upsert operation in MongoDB is utilized to salvage document into collection. If document matches query criteria and then it will perform update operation otherwise it will insert a new certificate into drove.
Upsert operation is useful while importing data from external source which volition update existing documents if matched otherwise it will insert new documents into collection.
Example: Upsert selection set for update
This operation offset searches for the document if not nowadays then inserts the new certificate into the database.
> db . car . update ( ... { proper noun : " Qualis " }, ... { ... proper name : " Qualis " , ... speed : 50 ... }, ... { upsert : truthful } ... ) WriteResult ({ " nMatched " : 0 , " nUpserted " : 1 , " nModified " : 0 , " _id " : ObjectId ( " 548d3a955a5072e76925dc1c " ) })
The car with the name Qualis is checked for beingness and if non, a certificate with car name "Qualis" and speed 50 is inserted into the database. The nUpserted with value "i" indicates a new document is inserted.
Q. Is there an "upsert" choice in the mongodb insert control?
The db.collection.insert()
provides no upsert possibility. Instead, mongo insert inserts a new document into a collection. Upsert is only possible using db.collection.update()
and db.drove.save()
.
Q. What is oplog?
The OpLog (Operations Log) is a special capped drove that keeps a rolling record of all operations that change the information stored in databases.
MongoDB applies database operations on the principal and so records the operations on the master's oplog. The secondary members then re-create and apply these operations in an asynchronous procedure. All replica set members contain a copy of the oplog, in the local.oplog.rs collection, which allows them to maintain the electric current state of the database.
Each operation in the oplog is idempotent. That is, oplog operations produce the same results whether applied once or multiple times to the target dataset.
Instance: Querying The OpLog
MongoDB shell version : ii.0 . iv connecting to : mongodb : 27017 / examination PRIMARY > use local PRIMARY > db . oplog . rs . find ()
MongoDB pushes the data to deejay lazily. It updates the immediately written to the periodical merely writing the data from periodical to disk happens lazily.
Q. How to perform a delete operation in MongoDB?
MongoDB'southward db.collection.deleteMany()
and db.collection.deleteOne()
method is used to delete documents from the drove. Delete operations exercise not drib indexes, even if deleting all documents from a drove. All write operations in MongoDB are diminutive on the level of a single document.
Example:
// Create Inventory Drove db . inventory . insertMany ( [ { item : " journal " , qty : 25 , size : { h : fourteen , w : 21 , uom : " cm " }, status : " A " }, { item : " notebook " , qty : 50 , size : { h : 8.five , w : 11 , uom : " in " }, condition : " P " }, { item : " paper " , qty : 100 , size : { h : viii.5 , westward : 11 , uom : " in " }, status : " D " }, { item : " planner " , qty : 75 , size : { h : 22.85 , w : thirty , uom : " cm " }, status : " D " }, { item : " postcard " , qty : 45 , size : { h : 10 , west : fifteen.25 , uom : " cm " }, condition : " A " }, ] ); // Delete Commands db . inventory . deleteMany ({}) // Delete All Documents db . inventory . deleteMany ({ status : " A " }) // Delete All Documents that Match a Status db . inventory . deleteOne ( { status : " D " } ) // Delete But One Document that Matches a Status
Q. If you remove a document from database, does MongoDB remove information technology from disk?
Yeah. If you remove a document from database, MongoDB volition remove it from disk too.
Q. Explicate the structure of ObjectID in MongoDB?
The ObjectId(<hexadecimal>
) form is the default primary cardinal for a MongoDB certificate and is usually found in the _id
field in an inserted document. It returns a new ObjectId value. The 12-byte ObjectId value consists of:
- a 4-byte timestamp value, representing the ObjectId's cosmos, measured in seconds since the Unix epoch
- a 5-byte random value
- a 3-byte incrementing counter, initialized to a random value
While the BSON format itself is piffling-endian, the timestamp and counter values are big-endian, with the virtually significant bytes actualization first in the byte sequence.
Create ObjectId
To create a new objectID manually within the MongoDB we can declare objectId()
as a method.
> newObjectId = ObjectId (); // Output ObjectId ( " 5349b4ddd2781d08c09890f3 " )
MongoDB provides three methods for ObjectId
Method | Description |
---|---|
ObjectId.getTimestamp() | Returns the timestamp portion of the object as a Date. |
ObjectId.toString() | Returns the JavaScript representation in the grade of a string literal "ObjectId(…)". |
ObjectId.valueOf() | Returns the representation of the object as a hexadecimal string. |
Q. What is a covered query in MongoDB?
The MongoDB covered query is one which uses an index and does not accept to examine any documents. An index will comprehend a query if it satisfies the following conditions:
- All fields in a query are function of an index.
- All fields returned in the results are of the same alphabetize.
- No fields in the query are equal to cypher
Since all the fields present in the query are part of an index, MongoDB matches the query conditions and returns the upshot using the same index without actually looking within the documents.
Case:
A collection inventory has the following alphabetize on the type and detail fields:
db . inventory . createIndex ( { type : 1 , detail : 1 } )
This index will comprehend the post-obit performance which queries on the type and item fields and returns merely the item field:
db . inventory . find ( { type : " food " , item : /^c/ }, { item : 1 , _id : 0 } )
Q. Why MongoDB is non preferred over a 32-bit system?
When running a 32-bit organization build of MongoDB, the total storage size for the server, including data and indexes, is 2 gigabytes. The reason for this is that the MongoDB storage engine uses memory-mapped files for performance.
If you are running a 64-bit build of MongoDB, there is about no limit to storage size.
Q. Can one MongoDB operation lock more than 1 database?
Yeah. Operations like db.copyDatabase()
, db.repairDatabase()
, etc. can lock more than than one databases involved.
Q. What is Sharding in MongoDB?
Sharding is a method for distributing data beyond multiple machines. MongoDB uses sharding to support deployments with very large data sets and high throughput operations.
Database systems with large information sets or loftier throughput applications can claiming the capacity of a single server. For example, high query rates can exhaust the CPU capacity of the server. Working gear up sizes larger than the system's RAM stress the I/O capacity of deejay drives. There are two methods for addressing arrangement growth: vertical and horizontal scaling.
1. Vertical Scaling
Vertical Scaling involves increasing the capacity of a unmarried server, such as using a more powerful CPU, adding more RAM, or increasing the amount of storage space.
two. Horizontal Scaling
Horizontal Scaling involves dividing the system dataset and load over multiple servers, adding boosted servers to increase capacity as required. While the overall speed or capacity of a single machine may not be high, each machine handles a subset of the overall workload, potentially providing better efficiency than a single high-speed high-capacity server.
MongoDB supports horizontal scaling through sharding
. A MongoDB sharded cluster consists of the post-obit components:
- Shards: Each shard contains a subset of the sharded data. Each shard tin can be deployed equally a replica set.
- Mongos: The mongos acts as a query router, providing an interface between client applications and the sharded cluster. Starting in MongoDB 4.4, mongos tin support hedged reads to minimize latencies.
- Config Servers: Config servers store metadata and configuration settings for the cluster.
Q. What is Aggregation in MongoDB?
Assemblage in MongoDB is an operation used to process the data that returns the computed results. Aggregation basically groups the data from multiple documents and operates in many means on those grouped data in order to return ane combined outcome.
Amass office groups the records in a collection, and tin be used to provide total number(sum), average, minimum, maximum etc out of the group selected. In social club to perform the aggregate part in MongoDB, amass () is the part to be used.
Syntax
db . collection_name . amass ( aggregate_operation )
MongoDB provides 3 ways to perform aggregation:
- the assemblage pipeline,
- the map-reduce function,
- and single purpose assemblage methods and commands.
MongoDB'southward aggregation framework is modeled on the concept of data processing pipelines. Documents enter a multi-stage pipeline that transforms the documents into an aggregated result.
Example
db . orders . aggregate ([ { $match : { status : " A " } }, { $group : { _id : " $cust_id " , total : { $sum : " $amount " } } } ]);
The $match
stage filters the documents by the status field and passes to the side by side stage those documents that have status equal to "A". The $group
phase groups the documents by the cust_id field to calculate the sum of the amount for each unique cust_id.
Expressions used past Amass function
Expression | Clarification |
---|---|
$sum | Summates the divers values from all the documents in a collection |
$avg | Calculates the boilerplate values from all the documents in a drove |
$min | Return the minimum of all values of documents in a collection |
$max | Return the maximum of all values of documents in a collection |
$addToSet | Inserts values to an array merely no duplicates in the resulting document |
$push | Inserts values to an array in the resulting certificate |
$first | Returns the first document from the source document |
$final | Returns the terminal document from the source document |
Q. Why are MongoDB data files big in size?
MongoDB preallocates information files to reserve infinite and avoid file system fragmentation when you setup the server.
Q. How can you isolate your cursors from intervening with the write operations?
Every bit cursor is not isolated during its lifetime, thus intervening write operations on a certificate may result in cursor that returns a document more than one time. The snapshot()
method tin can be used on a cursor to isolate the performance for a very specific example. snapshot()
traverses the index on the _id
field and guarantees that the query will render each certificate no more than in one case.
Brake:
- Nosotros cannot employ
snapshot()
with sharded collections. - Nosotros cannot use
snapshot()
withsort()
orhint()
cursor methods.
Q. At what interval does MongoDB write updates to the disk?
Past default configuration, MongoDB writes updates to the deejay every 60 seconds. However, this is configurable with the commitIntervalMs
and syncPeriodSecs
options.
Q. What happens if an index does not fit into RAM?
If the indexes does non fit into RAM, MongoDB reads information from deejay which is relatively very much slower than reading from RAM.
Indexes do non have to fit entirely into RAM in all cases. If the value of the indexed field increments with every insert, and most queries select recently added documents; then MongoDB just needs to keep the parts of the alphabetize that hold the most recent or "right-most" values in RAM. This allows for efficient alphabetize apply for read and write operations and minimize the amount of RAM required to support the alphabetize.
Instance: To check the size of indexes
> db . drove . totalIndexSize () // Output (in bytes) 4294976499
Q. Does MongoDB provide a facility to do text search?
MongoDB supports query operations that perform a text search of string content. To perform text search, MongoDB uses a text index
and the $text
operator.
Case
A collection stores with the post-obit documents:
db . stores . insert ( [ { _id : 1 , name : " Java Hut " , description : " Coffee and cakes " }, { _id : 2 , name : " Burger Buns " , description : " Gourmet hamburgers " }, { _id : 3 , name : " Coffee Store " , description : " Just coffee " }, { _id : 4 , name : " Clothes Clothes Dress " , description : " Discount article of clothing " }, { _id : 5 , proper name : " Coffee Shopping " , clarification : " Indonesian goods " } ] )
1. Text Index
MongoDB provides text indexes
to back up text search queries on string content. text indexes
can include any field whose value is a string or an assortment of string elements.
db . stores . createIndex ( { name : " text " , description : " text " } )
2. $text Operator
Utilize the $text
query operator to perform text searches on a collection with a text index. $text
will tokenize the search string using whitespace and well-nigh punctuation as delimiters, and perform a logical OR of all such tokens in the search string.
Case:
// Returns all stores containing any terms from the list "coffee", "store", and "java" db . stores . discover ( { $text : { $search : " coffee coffee shop " } } ) // Returns all documents containing "coffee store" db . stores . notice ( { $text : { $search : " \" coffee shop \" " } } ) // Returns all stores containing "java" or "shop" but not "coffee" db . stores . find ( { $text : { $search : " java shop -coffee " } } )
Q. How does Journaling work in MongoDB?
Mongod primarily hosts the write operations in memory in shared view. Information technology is chosen shared considering information technology has memory mapping in actual disc. In this procedure, a write operation occurs in mongod, which so creates changes in individual view. The first block is retention and the second block is "my disc". After a specified interval, which is called a "journal commit interval", the private view writes those operations in journal directory (residing in the disc).
One time the journal commit happens, mongod pushes data into shared view. As part of the process, it gets written to bodily information directory from the shared view (as this procedure happens in groundwork). The bones advantage is, we have a reduced cycle from lx seconds to 200 milliseconds.
In a scenario where an abruption occurs at whatever point of time or wink disc remains unavailable for last 59 seconds , then when the next time mongod starts, it basically replays all write operation logs and writes into the actual information directory.
Q. Is MongoDB schema-less?
As a NoSQL database, MongoDB is considered schemaless because it does not require a rigid, pre-divers schema like a relational database. The database management arrangement (DBMS) enforces a partial schema every bit data is written, explicitly listing collections and indexes.
MongoDB is a document based database, which does not use the concept of tables and columns, instead of which it uses the concept of documents and collections. All the referential data with respect to different modules will be stored as one drove. More over the BSON information structure used by MongoDB can easily take varying sets of data and fields with different types.
When nosotros say schemaless, nosotros actually mean dynamically typed schema, as opposed to statically typed schemas as available in RDBMS(SQL) databases. JSON is a completely schema free data structure, as opposed to XML which allows you lot to specify XSD if you need.
Q. What is a Storage Engine in MongoDB?
The storage engine is the component of the database that is responsible for managing how data is stored, both in memory and on disk. MongoDB supports multiple storage engines, as different engines perform better for specific workloads.
Example: command to find storage engine
> db . serverStatus (). storageEngine // Output { " name " : " wiredTiger " , " supportsCommittedReads " : true , " oldestRequiredTimestampForCrashRecovery " : Timestamp ( 0 , 0 ), " supportsPendingDrops " : truthful , " dropPendingIdents " : NumberLong ( 0 ), " supportsTwoPhaseIndexBuild " : true , " supportsSnapshotReadConcern " : true , " readOnly " : false , " persistent " : true , " backupCursorOpen " : fake }
MongoDB supports mainly 3 storage engines whose performance differ in accord to some specific workloads. The storage engines are:
- WiredTiger Storage Engine
- In-Retentiveness Storage Engine
- MMAPv1 Storage Engine
1. WiredTiger Storage Engine
WiredTiger
is the default storage engine starting in MongoDB 3.ii. Information technology is well-suited for most workloads and is recommended for new deployments. WiredTiger provides a certificate-level concurrency model, checkpointing, and pinch, among other features. The WiredTiger storage engine has both configurations of a B-Tree
Based Engine and a Log Structured Merge Tree
Based Engine.
2. In-Retentiveness Storage Engine
In-Memory Storage Engine is available in MongoDB Enterprise. Rather than storing documents on-disk, information technology retains them in-memory for more predictable data latencies.
3. MMAPv1 Storage Engine
MMAPv1 is a B-tree based arrangement which powers many of the functions such as storage interaction and memory management to the operating system. Its name comes from the fact that it uses memory mapped files to access data. It does so past straight loading and modifying file contents, which are in a virtual memory through a mmap()
syscall
methodology.
Q. How to condense large volumes of data in Mongodb?
compact
Rewrites and defragments all information and indexes in a collection. On WiredTiger
databases, this command will release unneeded deejay space to the operating system. This control will perform a compaction "in-line".
MongoDB compresses the files past:
- copying the files to a new location
- looping through the documents and re-ordering / re-solving them
- replacing the original files with the new files
Syntax
{ compact : < collection name > }
Q. Explicate relationships in MongoDB?
Relationships in MongoDB are used to specify how 1 or more documents are related to each other. In MongoDB, the relationships tin be modelled either by Embedded manner or by using the Reference approach. These relationships can exist of the following forms:
- I to One
- One to Many
- Many to Many
Example: Let united states consider the case of storing addresses for users. So, ane user tin can have multiple addresses making this a ane:N
relationship.
User Drove
{ " _id " : ObjectId ( " 52ffc33cd85242f436000001 " ), " name " : " Alex M " , " contact " : " 987654321 " , " dob " : " 01-01-1990 " }
Accost Collection
{ " _id " : ObjectId ( " 52ffc4a5d85242602e000000 " ), " building " : " 22 A, Indiana Apt " , " pincode " : 123456 , " city " : " Los Angeles " , " land " : " California " }
1. Modeling Embedded Relationships
In the embedded approach, we will embed the address document inside the user document.
> db . users . insert ({ { " _id " : ObjectId ( " 52ffc33cd85242f436000001 " ), " contact " : " 987654321 " , " dob " : " 01-01-1991 " , " proper noun " : " Alex K " , " address " : [ { " building " : " 22 A, Indiana Apt " , " pincode " : 123456 , " urban center " : " Los Angeles " , " state " : " California " }, { " edifice " : " 170 A, Acropolis Apt " , " pincode " : 456789 , " metropolis " : " Chicago " , " state " : " Illinois " } ] } })
This approach maintains all the related information in a single certificate, which makes it easy to retrieve and maintain. The whole certificate can be retrieved in a unmarried query such as −
> db . users . findOne ({ " name " : " Alex K " },{ " address " : ane })
The drawback is that if the embedded document keeps on growing too much in size, it tin can impact the read/write performance.
2. Modeling Referenced Relationships
This is the approach of designing normalized relationship. In this approach, both the user and address documents volition be maintained separately but the user document will contain a field that will reference the address document'southward id field.
{ " _id " : ObjectId ( " 52ffc33cd85242f436000001 " ), " contact " : " 987654321 " , " dob " : " 01-01-1991 " , " name " : " Alex K " , " address_ids " : [ ObjectId ( " 52ffc4a5d85242602e000000 " ), ObjectId ( " 52ffc4a5d85242602e000001 " ) ] }
With this arroyo, we will demand two queries: get-go to fetch the address_ids
fields from user certificate and 2d to fetch these addresses from accost collection.
> var upshot = db . users . findOne ({ " proper noun " : " Alex K " },{ " address_ids " : i }) > var addresses = db . address . notice ({ " _id " :{ " $in " : result [ " address_ids " ]}})
Q. What is apply of capped drove in MongoDB?
Capped collections are fixed-size collections that back up high-throughput operations that insert and retrieve documents based on insertion order. Capped collections work in a way similar to round buffers
: once a collection fills its allocated space, information technology makes room for new documents by overwriting the oldest documents in the collection.
Capped collections restrict updates to the documents if the update results in increased document size. Since capped collections store documents in the society of the deejay storage, it ensures that the document size does not increment the size allocated on the disk. Capped collections are best for storing log information, enshroud information, or any other high volume data.
Instance:-
> db . createCollection ( " log " , { capped : true , size : 100000 } ) // specify a maximum number of documents for the collection > db . createCollection ( " log " , { capped : true , size : 5242880 , max : 5000 } ) // check whether a collection is capped or not > db . cappedLogCollection . isCapped () // convert existing collection to capped > db . runCommand ({ " convertToCapped " : " posts " , size : 10000 }) // Querying Capped Collection > db . cappedLogCollection . find (). sort ({ $natural : - 1 })
Q. What is splitting in MongoDB?
Splitting is a process that keeps chunks from growing also large. When a clamper grows beyond a specified clamper size, or if the number of documents in the chunk exceeds Maximum Number of Documents Per Chunk to Migrate, MongoDB splits the chunk based on the shard key values the chunk represent.
Q. Explain what is horizontal scalability in mongodb?
Horizontal Scaling involves dividing the system dataset and load over multiple servers, adding additional servers to increase chapters every bit required. While the overall speed or capacity of a single machine may not exist high, each machine handles a subset of the overall workload, potentially providing better efficiency than a unmarried high-speed loftier-capacity server. Expanding the chapters of the deployment merely requires adding boosted servers every bit needed, which tin exist a lower overall cost than loftier-stop hardware for a single automobile. The trade off is increased complexity in infrastructure and maintenance for the deployment.
bergmannhincir1988.blogspot.com
Source: https://learning-zone.github.io/mongodb-interview-questions/
0 Response to "Failed Write Operations to Be Processes Again in Flockdb"
Post a Comment