MongoDB Interview Questions with Answers
MongoDB is an open source platform written in C++ and has a very easy setup environment. It is a cross-platform, document-oriented and non-structured database.
MongoDB provides high performance, high availability, and auto-scaling.
1) what is MongoDB?
Mongo-DB is a document database which provides high performance, high availability and easy scalability
2) What is a Database?
A database is a collection of information that is organized so that it can be easily accessed, managed and updated.
Computer databases typically contain aggregations of data records or files, containing information
about sales transactions or interactions with specific customers.
3) 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.
4) What is sharding in MongoDB?
The procedure of storing data records across multiple machines is referred as Sharding. It is a MongoDB approach to meet the demands of data growth. It is the horizontal partition of data in a database or
search engine. Each partition is referred as shard or database shard.
5) How can you see the connection used by Mongos?
To see the connection used by Mongos use db_adminCommand (“connPoolStats”);
6) Explain what is a replica set?
A replica set is a group of mongo instances that host the same data set. In replica set, one node is primary, and another is secondary. From primary to the secondary node all data replicates.
7) How replication works in MongoDB?
Across multiple servers, the process of synchronizing data is known as replication. It provides redundancy and increase data availability with multiple copies of data on different database server.
Replication helps in protecting the database from the loss of a single server.
8) While creating Schema in MongoDB what are the points need to be taken in consideration?
Points need to be taken in consideration are
- Design your schema according to user requirements
- Combine objects into one document if you use them together. Otherwise, separate them
- Do joins while write, and not when it is on read
- For most frequent use cases optimize your schema
- Do complex aggregation in the schema
9) Compare MongoDB and Cassandra
MongoDB:
- data mode are document
- database scalability is read only
- query of the data is multi-index
Cassandra:
- data mode are big table like
- database scalability is write only
- query of the data is using key or scana
10) Mention what is Object ld composed of?
- Timestamp
- Client machine ID
- Client process ID
- byte incremented counter
11) What are alternatives to MongoDB?
- Cassandr
- CouchDB
- Redis
- Riak
12) What are all the advantage of MongoDB?
- It is a Schema less.
- Structure of a single object is clear.
- No complex joins.
- Easy of scale-out
13) What is replication and why we need?
It is the process of synchronizing data across multiple server. It provides redundancy and increases data availability with multiple copies of data on different database server.WHY: To keep data safe,
High availability of data, disaster recovery, No downtime for maintenance.
14) What is CRUD?
CRUD operations are Create, Read, Update, Delete.
15) What are the disadvantages of MongoDB?
- A 32-bit edition has 2GB data limit. After that it will corrupt the entire DB, including the existing data. A 64-bit edition won’t suffer from this bug/feature.
- Default installation of MongoDB has asynchronous and batch commits turned on. Meaning, it lies when asked to store something in DB and commits all changes in a batch at a later time in future. If there is a server crash or power failure, all those commits buffered in memory will be lost. This functionality can be disabled, but then it will perform as good as or worse than MySQL.
- MongoDB is only ideal for implementing things like analytics/caching where impact of small data loss is negligible.
- In MongoDB, it’s difficult to represent relationships between data so you end up doing that manually by creating another table to represent the relationship between rows in two or more tables.
16) What MongoDB will provide consistency?
The writer makes use of MongoDB is he that readeth, and the bolts of it, after allowing to access one considers arbitrary or that any one that my readers may at the same time as the collection of a
database. And it always brings access to private and hated by others.
17) What does ObjectId consist of?
It consists of the following:
- Timestamp
- Client machine ID
- Client process ID
- Byte incremented counter
18) What is the difference between MongoDB and Cassandra?
Difference between MongoDB and Cassandra:
- MongoDB is cross-platform document-oriented database system while Cassandra is high performance distributed database system.
- MongoDB is written in C++ while Cassandra is written in Java.
- MongoDB is easy to administer in the case of failure while Cassandra provides high availability with no single point of failure.
19) What are the key features of mongodb?
there are three main featured:
- automatic scaling
- High performance
- high availability
20) what are the points need to be taken in consideration?
Points need to be taken in consideration are:
- Design your schema according to user requirements
- Combine objects into one document if you use them together. Otherwise, separate them
- Do joins while write, and not when it is on read
- For most frequent use cases optimize your schema
- Do complex aggregation in the schemaThis blog will help you get a better understanding of Difference Between Cassandra and MongoDB!
21) How to perform join operations in MongoDB?
From MongoDB 3.2 onwards we can perform join operation. The new $lookup operator added with the aggregation pipeline is
essentially similar to a left outer join. This is an example of the same:
- {
- $lookup:
- {
- from: <collection to join>,
- localField: <field from the input documents>,
- foreignField: <field from the documents of the "from" collection>,
- as: <output array field>
- }
- }