Saturday, April 6, 2019

MongoDB Interview Questions and Answers

MongoDB is a document-oriented database. MongoDB is a powerful, scalable and much more data flexible database which provides high performance in case of a large volume of data and also leads to a NoSQL Database. MongoDB stores the data in a BSON format. This article contains the top 20 MongoDB interview questions and answers, in order to prepare you for the interview.
  1. What is MongoDB?

    MongoDB is a document-oriented database. MongoDB is a powerful, scalable and much more data flexible database which provides high performance in case of a large volume of data and also leads to a NoSQL Database. MongoDB stores the data in a BSON format.
  2. What is NoSQL Database?

    NoSQL stands for Not Only SQL. NoSQL is a category of Database Management System (DBMS) which does maintain all the rules of traditional RDBMS systems. It also does not use the conventional SQL syntaxes to fetch the data from the database. This type of database systems is typically used in case of a very large volume of data. Some of well-known NoSQL database systems are – Cassandra, BigTable, DynamoDB, MongoDB etc.
  3. What are the types of NoSQL Database?

    There are four types of NoSQL Database is available:

    Document Database

    This type of NoSQL database is always based on a Document-Oriented approach to store data. The main objective of the Document Database is to store all data of a single entity as a document and all documents can be stored as a Collections. Some example of the Document Database – MongoDB, CosmosDB, CouchDB, PostgreSQL etc.

    Key-Value Database

    This type of database stored data in a schema-less way since key-value storage is the simplest way of storing data. A key can be a point to any type of data, like an object, string or any other types of data. The main advantages of these databases are easy to implement and add data into it. Example – Redis, DynamoDB etc.

    Column Store Database

    These types of databases store data in columns within a keyspace. The key space is always defined on a unique name, value and a timestamp. Example – Cassandra, BigTable, HBase, Vertica, HyperTable.

    Graph Store Database

    These types of database mainly designed for data that can be easily represented as graph data. This means that data are interconnected with an undetermined number of data relations between them like family and social relations etc. Example – AllegroDB, GraphDB, OrientDB, Titan.
  4. What are the advantages of MongoDB?

    The main advantages of MongoDB are:
    1. It can deal with a high volume of data.
    2. It supports cross-platform
    3. It provides High Performance
    4. It is easily scalable
    5. It does not require any complex joins to retrieve data
    6. It supports both type of scaling – Horizontal & Vertical
    7. It is available on any cloud-based environment like AzureAWS etc
  5. What are Documents?

    The document is actually being the heart of MongoDB. In simple word, the document is an ordered set of keys with associated values. It is similar to the table rows in the RDBMS systems. It always maintains dynamic scheme so that it does not require any predefined structure or fields.
  6. What is Collection?

    A collection is a group of documents. The collection is basically representing the table objects in the RDBMS systems.
  7. What are Dynamic Schemas?

    In MongoDB, Collections always have dynamic Schemas. Dynamic Schemas means the documents within a single collection may contain different types of structure or shapes. For an example, both the below documents can be stored in a single collection:
    1. {"message" : "Hello World"}
    2. {"id" : 10, "description" : "India"}
  8. What is Mongo Shell?

    MongoDB Shell is a JavaScript shell which allows us to interactions with MongoDB instances using the command line. It is very much useful to perform any administrative work along with any other data operations related commands. Mongo Shell is automatically installed when we installed the MongoDB in our computers.
  9. What is MongoDB?

    MongoDB always runs as a network server so that clients can connect with the server and perform operations on it. So, to start the server, we need to execute the command mongodb. It basically starts the host process for the database and once it is started, it continued running in the background.
  10. Where we can use MongoDB?

    MongoDB can use used in the following areas –
    • Content Management System
    • Mobile Apps where data volume is very large and required high readability of data
    • Data Management
    • Big Data
  11. Which languages supports MongoDB?

    There are several languages which are supported by MongoDB like
    • C++
    • C
    • C#
    • Java
    • Node.Js
    • Perl
    • PHP etc
  12. What data types are supported by MongoDB?

    MongoDB supports a wide range of data types a value in documents. Below is the available data types in the MongoDB.
    Data Types
    Descriptions
    String
    It is most commonly used data types. A string must be UTF-8 valid in MongoDB
    Integer
    It is used to store numeric values. It may either 32 bit or 64 bits.
    Boolean
    It is used to store Boolean data types. It's valued either true or false.
    Double
    It is used to store floating point values.
    Arrays
    This data type is used to store a list or multiple values in a single key
    Objects
    This data type is used to store embedded data
    Null
    It is used to store null data
    Date
    This data type is used to store current date or time value in Unix time format.
  13. What is ObjectId in MongoDB?

    Each document stored in MongoDB must contain an “_id” key. The default value type of the “_id” is ObjectId. In a single collection, every document always contains a unique value of the “_id” field, so that every document can be identified easily. ObjectId always uses 12 bytes of storage. It always represents 24 hexadecimal digit string values.
  14. ow to Insert multiple documents in MongoDB?

    To Insert multiple documents at a time, we need to use –
    1. db.<CollectionName>.insert(
    2. [
    3. {“Id”:1,”name”:”Kamalesh”,”Code”:”A001”},
    4. {“Id”:2,”name”:”Sumit”,”Code”:”A002”}
    5. ]
    6. )
  15. What is Capped Collection in MongoDB?

    In MongoDB, normal collections are created dynamically at the time of data insertion and automatically grow in size so that additional data can be fit. But in MongoDB, there is also another type of collections which a pre-created collection is always along with fixed size. These types of collection are known as Capped Collection. But one question is that What will happen when the size of the capped collection is exceeded? Actually, capped collection always behaves like circular queues. So, when the capped collection is filled up and new a document is inserted then the oldest one document will be deleted automatically. So, to create a capped collection, the command will be –
    1. db.createCollection(“CollectionName”, {“capped”:true, size : 100000})
  16. What is the Full-Text Index?

    Full-Text Index is one of the special types of Index in MongoDB for searching text within the documents. Full-Text index always gives us the ability to search text quickly. But this type of index is basically expensive for uses concern. So, creating a full-text index on a busy collection can overload the MongoDB Server. So it is always recommended to use this type of index in an offline mode. To create a full-text index, the command is –
    1. db.<CollectionName>.ensureIndex({“name : text”})
  17. What is GridFS?

    Since in MongoDB, every document size limit is 16 MB. So, if we want to insert any large binary data file, then we need to use GridFS. GridFS is a mechanism through which we can store any type of large file data like an audio file, video file or image etc. It is basically just like a file system to store these large files and also, its related data stored in the MongoDB collection.
  18. What is the purpose of using $group?

    $group syntax is used to bundle or group the documents of a collection on the basis of the one or more fields. So, if we want to group the data on depends on one or more than one fields, then we need to pass those fields name within the group method to create a group key and normally the group key name is “_id”.
    1. {"$group" : {"_id" : {"state" : "$state", "city" : "$city"}}}
    We can use any type of arithmetic operator with the group command as below –
    1. db.sales.aggregate(
    2. {
    3. "$group" : {
    4. "_id" : "$country",
    5. "totalRevenue" : {"$average" : "$revenue"},
    6. "numSales" : {"$sum" : 1}
    7. }
    8. })
  19. What is Replication?

    Replication is the process which is responsible for keeping identical copies of our data in multiple servers and is always a recommended process for any type of production server. Replication always keeps our database safe even if the database server crashed or data corrupted. With the additional copies, MongoDB maintains the copy of the data for disaster recovery, reporting or backup purpose.
  20. Why Replication is required in MongoDB?

    In MongoDB, replication is required due to the following reason –
    • To provide always the availability of data
    • To secure our application data
    • Recover the data from any type of disaster recovery
    • In the Replication process, no downtime requires maintenance like backup, index rebuilds etc.
    • Replication can provide us with a read scaling means it will provide us with a copy of data only for read purpose.

No comments:

Post a Comment

Get max value for identity column without a table scan

  You can use   IDENT_CURRENT   to look up the last identity value to be inserted, e.g. IDENT_CURRENT( 'MyTable' ) However, be caut...