Examness

Database

MongoDB इंटरव्यू प्रश्न

Documents, aggregation, indexing, replication and sharding.

65 प्रश्न

  1. 1.

    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" : true,
        "dropPendingIdents" : NumberLong(0),
        "supportsTwoPhaseIndexBuild" : true,
        "supportsSnapshotReadConcern" : true,
        "readOnly" : false,
        "persistent" : true,
        "backupCursorOpen" : false
    }

    MongoDB supports mainly 3 storage engines whose performance differ in accordance to some specific workloads. The storage engines are:

    • WiredTiger Storage Engine
    • In-Memory Storage Engine
    • ~~MMAPv1 Storage Engine~~ ( This Storage Engine has been deprecated after MongoDB 4.2 )

    1. WiredTiger Storage Engine

    WiredTiger is the default storage engine starting in MongoDB 3.2. It is well-suited for most workloads and is recommended for new deployments. WiredTiger provides a document-level concurrency model, checkpointing, and compression, 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-Memory Storage Engine

    In-Memory Storage Engine is available in MongoDB Enterprise. Rather than storing documents on-disk, it retains them in-memory for more predictable data latencies.

    3. MMAPv1 Storage Engine

    MMAPv1 is a B-tree based system 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 by directly loading and modifying file contents, which are in a virtual memory through a mmap() syscall methodology.

    ↥ back to top

  2. 2.

    What is a covered query in MongoDB?

    शुरुआती

    The MongoDB covered query is one which uses an index and does not have to examine any documents. An index will cover a query if it satisfies the following conditions:

    • All fields in a query are part of an index.
    • All fields returned in the results are of the same index.
    • No fields in the query are equal to null

    Since all the fields present in the query are part of an index, MongoDB matches the query conditions and returns the result using the same index without actually looking inside the documents.

    Example:

    A collection inventory has the following index on the type and item fields:

    db.inventory.createIndex( { type: 1, item: 1 } )

    This index will cover the following operation which queries on the type and item fields and returns only the item field:

    db.inventory.find(
       { type: "food", item:/^c/ },
       { item: 1, _id: 0 }
    )

    ↥ back to top

  3. 3.

    What are the differences between MongoDB and SQL-SERVER?

    शुरुआती
    • The MongoDB store the data in documents with JSON format but SQL store the data in Table format.
    • The MongoDB provides high performance, high availability, easy scalability etc. rather than SQL Server.
    • In the MongoDB, we can change the structure simply by adding, removing column from the existing documents.

    MongoDB and SQL Server Comparision Table:

    Base of ComparisonMS SQL ServerMongoDB
    Storage ModelRDBMSDocument-Oriented
    JoinsYesNo
    TransactionACIDMulti-document ACID Transactions with snapshot isolation
    Agile practicesNoYes
    Data SchemaFixedDynamic
    ScalabilityVerticalHorizontal
    Map ReduceNoYes
    LanguageSQL query languageJSON Query Language
    Secondary indexYesYes
    TriggersYesYes
    Foreign KeysYesNo
    ConcurrencyYesyes
    XML SupportYesNo

    ↥ back to top

  4. 4.

    Describe what a MongoDB database is

    शुरुआती

    A MongoDB database is a document-oriented, NoSQL database consisting of collections, each of which in turn comprise documents.

    Core Concepts

  5. 5.

    What is upsert operation in MongoDB?

    शुरुआती

    Upsert operation in MongoDB is utilized to save document into collection. If document matches query criteria then it will perform update operation otherwise it will insert a new document into collection.

    Upsert operation is useful while importing data from external source which will update existing documents if matched otherwise it will insert new documents into collection.

    Example: Upsert option set for update

    This operation first searches for the document if not present then inserts the new document into the database.

    
    > db.car.update(
    ...    { name: "Qualis" },
    ...    {
    ...       name: "Qualis",
    ...       speed: 50
    ...    },
    ...    { upsert: true }
    ... )
    WriteResult({
    	"nMatched" : 0,
    	"nUpserted" : 1,
    	"nModified" : 0,
    	"_id" : ObjectId("548d3a955a5072e76925dc1c")
    })

    The car with the name Qualis is checked for existence and if not, a document with car name "Qualis" and speed 50 is inserted into the database. The nUpserted with value "1" indicates a new document is inserted.

    ↥ back to top

  6. 6.

    What is an Embedded MongoDB Document?

    शुरुआती

    An embedded, or nested, MongoDB Document is a normal document that is nested inside another document within a MongoDB collection. Embedding connected data in a single document can reduce the number of read operations required to obtain data. In general, we should structure our schema so that application receives all of its required information in a single read operation.

    Example:

    In the normalized data model, the address documents contain a reference to the patron document.

    // patron document
    {
       _id: "joe",
       name: "Joe Bookreader"
    }
    
    // address documents
    {
       patron_id: "joe", // reference to patron document
       street: "123 Fake Street",
       city: "Faketon",
       state: "MA",
       zip: "12345"
    }
    
    {
       patron_id: "joe",
       street: "1 Some Other Street",
       city: "Boston",
       state: "MA",
       zip: "12345"
    }

    Embedded documents are particularly useful when a one-to-many relationship exists between documents. In the example shown above, we see that a single customer has multiple addresses associated with him. The nested document structure makes it easy to retrieve complete address information about this customer with just a single query.

    ↥ back to top

  7. 7.

    What is MongoDB?

    शुरुआती

    MongoDB is a document-oriented NoSQL database used for high volume data storage. Instead of using tables and rows as 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.

    Key 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:

    1. _id: The _id field represents a unique value in the MongoDB document. The _id field is like the document\'s primary key. If you create a new document without an _id field, MongoDB will automatically create the field.

    2. Collection: This is a grouping of MongoDB documents. A collection is the equivalent of a table which is created in any other RDMS such as Oracle.

    3. Cursor: This is a pointer to the result set of a query. Clients can iterate through a cursor to retrieve results.

    4. Database: This is a container for collections like in RDMS wherein it is a container for tables. Each database gets its own set of files on the file system. A MongoDB server can store multiple databases.

    5. Document: A record in a MongoDB collection is basically called 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 zero or more fields. Fields are analogous to columns in relational databases.

    Example:

    Connecting MongoDB Cloud using MongoDB Compass

    [Read More]

    ↥ back to top

  8. 8.

    What are the MongoDB commands for deleting documents?

    शुरुआती

    MongoDB offers several methods for deleting documents.

    Deletion Methods in MongoDB

    1. deleteOne(): Deletes the first matched document.
    1. deleteMany(): Removes all matching documents.
    1. remove(): Legacy function; use deleteOne() or deleteMany() instead.

    General Syntax

    • For deleteOne(), the syntax is:
    • db.collection.deleteOne({filter}, {options})
    • For deleteMany(), the syntax is:
    • db.collection.deleteMany({filter}, {options})

    Code Example: Deleting One or Many

    Here is the MongoDB shell script:

    // Connect to the database
    use myDB;
    
    // Delete a single document from 'myCollection'
    db.myCollection.deleteOne({ name: "Document1" });
    
    // Delete all documents from 'myCollection' with the condition 'age' greater than 25
    db.myCollection.deleteMany({ age: { $gt: 25 } });

    Explore all 100 answers here 👉 Devinterview.io - MongoDB

  9. 9.

    What is Replication in Mongodb?

    शुरुआती

    Replication exists primarily to offer data redundancy and high availability. It maintain the durability of data by keeping multiple copies or replicas of that data on physically isolated servers. Replication allows to increase data availability by 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 data. If the primary server fails (through a crash or system 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 primary node.

    ↥ back to top

  10. 10.

    What is "Namespace" in MongoDB?

    शुरुआती

    MongoDB stores BSON (Binary Interchange and Structure Object Notation) objects in the collection. The concatenation of the collection name and database name is called a namespace

    ↥ back to top

  11. 11.

    What are Indexes in MongoDB?

    शुरुआती

    Indexes support the efficient execution of queries in MongoDB. Without indexes, MongoDB must perform a collection scan, i.e. scan every document in a collection, to select those documents that match the query statement. If an appropriate index exists for a query, MongoDB can use the index to limit the number of documents it must inspect.

    Indexes are special data structures that store a small portion of the collection\'s data set in an easy to traverse form. The index stores the value of a specific field or set 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 return sorted results by using the ordering in the index.

    Example:

    The createIndex() method only creates an index if an index of the same specification does not already exist. The following example ( using Node.js ) creates a single key descending index on the name field:

    collection.createIndex( { name : -1 }, function(err, result) {
       console.log(result);
       callback(result);
    }

    ↥ back to top

  12. 12.

    Why are MongoDB data files large in size?

    शुरुआती

    MongoDB preallocates data files to reserve space and avoid file system fragmentation when you setup the server.

    ↥ back to top

  13. 13.

    What is splitting in MongoDB?

    शुरुआती

    Splitting is a process that keeps chunks from growing too large. When a chunk grows beyond a specified chunk 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.

  14. 14.

    What is oplog?

    शुरुआती

    The OpLog (Operations Log) is a special capped collection that keeps a rolling record of all operations that modify the data stored in databases.

    MongoDB applies database operations on the primary and then records the operations on the primary\'s oplog. The secondary members then copy and apply these operations in an asynchronous process. All replica set members contain a copy of the oplog, in the local.oplog.rs collection, which allows them to maintain the 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.

    Example: Querying The OpLog

    MongoDB shell version: 2.0.4
    connecting to: mongodb:27017/test
    PRIMARY> use local
    PRIMARY> db.oplog.rs.find()

    ↥ back to top

  15. 15.

    What is Replica Set in MongoDB?

    शुरुआती

    It is a group of mongo processes that maintain same data set. Replica sets provide redundancy and high availability, and are the basis for all production deployments. A replica set contains a primary node and multiple secondary nodes.

    The primary node receives all write operations. A replica set can have only 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\'s data set. If the primary is unavailable, an eligible secondary will hold an election to elect itself the new primary.

    ↥ back to top

  16. 16.

    What is the syntax to insert a document into a MongoDB collection?

    शुरुआती

    To insert a document into a MongoDB collection, you can use the `insertOne()` method, which accepts the document as an argument:

    db.collectionName.insertOne({
      key1: "value1",
      key2: 2,
      key3: [1, 2, 3],
      key4: { nestedKey: "nestedValue" }
    });

    Alternatively, you can use the `insertOne()` method, supply an array of documents with `insertMany()`:

    db.collectionName.insertMany([
      { key: "value1" },
      { key: "value2" }
    ]);
  17. 17.

    What is use of capped collection in MongoDB?

    शुरुआती

    Capped collections are fixed-size collections that support high-throughput operations that insert and retrieve documents based on insertion order. 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 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 order of the disk storage, it ensures that the document size does not increase the size allocated on the disk. Capped collections are best for storing log information, cache data, or any other high volume data.

    Example:

    >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})

    ↥ back to top

  18. 18.

    What is the default port on which MongoDB listens?

    शुरुआती

    The default port number for MongoDB is 27017. While it is possible to run multiple instances of MongoDB on the same machine, each instance must have its unique port number to ensure they don't conflict.

  19. 19.

    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-defined indexes like single field index. A single field index is used to create an index on the single field of a document. With single field index, MongoDB can traverse in ascending and descending order. By default, each collection has a single field index automatically created on the _id field, the primary key.

    Example:

    {
      "_id": 1,
      "person": { name: "Alex", surname: "K" },
      "age": 29,
      "city": "New York"
    }

    We can define, a single field index on the age field.

    db.people.createIndex( {age : 1} ) // creates an ascending index
    
    db.people.createIndex( {age : -1} ) // creates a descending index

    With this kind of index we can improve all the queries that find documents with a condition and the age field, like the following:

    db.people.find( { age : 20 } )
    db.people.find( { name : "Alex", age : 30 } )
    db.people.find( { age : { $gt : 25} } )

    2. Compound Index:

    A compound index is an index on multiple fields. Using the same people collection we can create a compound index combining the city and age field.

    db.people.createIndex( {city: 1, age: 1, person.surname: 1  } )

    In this case, we have created a compound index where the first entry is the value of the city field, the second is the value of the age field, and the third is the person.name. All the fields here are defined in ascending order.

    Queries such as the following can benefit from the index:

    db.people.find( { city: "Miami", age: { $gt: 50 } } )
    db.people.find( { city: "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 index on an array, MongoDB will create an index entry for every element.

    Example:

    {
       "_id": 1,
       "person": { name: "John", surname: "Brown" },
       "age": 34,
       "city": "New York",
       "hobbies": [ "music", "gardening", "skiing" ]
     }

    The multikey index can be created as:

    db.people.createIndex( { hobbies: 1} )

    Queries such as these next examples will use the index:

    db.people.find( { hobbies: "music" } )
    db.people.find( { hobbies: "music", hobbies: "gardening" } )

    4. Geospatial Index:

    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. 2d indexes use planar geometry when returning results and 2dsphere indexes use spherical geometry to return results.

    5. Text Index:

    It is another type of index that is supported by MongoDB. Text index supports searching for string content in a collection. These index types do not store language-specific stop words (e.g. "the", "a", "or"). Text indexes restrict the words in a collection to only store root words.

    Example:

    Let\'s insert some sample documents.

    var entries = db.people("blogs").entries;
    entries.insert( {
      title : "my blog post",
      text : "i am writing a blog. yay",
      site: "home",
      language: "english" });
    entries.insert( {
      title : "my 2nd post",
      text : "this is a new blog i am typing. yay",
      site: "work",
      language: "english" });
    entries.insert( {
      title : "knives are Fun",
      text : "this is a new blog i am writing. yay",
      site: "home",
      language: "english" });

    Let\'s define create the text index.

    var entries = db.people("blogs").entries;
    entries.ensureIndex({title: "text", text: "text"}, { weights: {
        title: 10,
        text: 5
      },
      name: "TextIndex",
      default_language: "english",
      language_override: "language" });

    Queries such as these next examples will use the index:

    var entries = db.people("blogs").entries;
    entries.find({$text: {$search: "blog"}, site: "home"})

    6. 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.

    ↥ back to top

  20. 20.

    Can one MongoDB operation lock more than one database?

    मध्यम

    Yes. Operations like db.copyDatabase(), db.repairDatabase(), etc. can lock more than one databases involved.

    ↥ back to top