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.
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.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.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.What are the advantages of MongoDB?
The main advantages of MongoDB are:- It can deal with a high volume of data.
- It supports cross-platform
- It provides High Performance
- It is easily scalable
- It does not require any complex joins to retrieve data
- It supports both type of scaling – Horizontal & Vertical
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.What is Collection?
A collection is a group of documents. The collection is basically representing the table objects in the RDBMS systems.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:- {"message" : "Hello World"}
- {"id" : 10, "description" : "India"}
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.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.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
Which languages supports MongoDB?
There are several languages which are supported by MongoDB like- C++
- C
- C#
- Java
- Node.Js
- Perl
- PHP etc
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 TypesDescriptionsStringIt is most commonly used data types. A string must be UTF-8 valid in MongoDBIntegerIt is used to store numeric values. It may either 32 bit or 64 bits.BooleanIt is used to store Boolean data types. It's valued either true or false.DoubleIt is used to store floating point values.ArraysThis data type is used to store a list or multiple values in a single keyObjectsThis data type is used to store embedded dataNullIt is used to store null dataDateThis data type is used to store current date or time value in Unix time format.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.ow to Insert multiple documents in MongoDB?
To Insert multiple documents at a time, we need to use –- db.<CollectionName>.insert(
- [
- {“Id”:1,”name”:”Kamalesh”,”Code”:”A001”},
- {“Id”:2,”name”:”Sumit”,”Code”:”A002”}
- ]
- )
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 –- db.createCollection(“CollectionName”, {“capped”:true, “size” : 100000})
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 –- db.<CollectionName>.ensureIndex({“name” : “text”})
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.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”.- {"$group" : {"_id" : {"state" : "$state", "city" : "$city"}}}
We can use any type of arithmetic operator with the group command as below –- db.sales.aggregate(
- {
- "$group" : {
- "_id" : "$country",
- "totalRevenue" : {"$average" : "$revenue"},
- "numSales" : {"$sum" : 1}
- }
- })
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.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