Interview question of mongoDB set 1/MongoDB Interview Questions and Answers for Freshers & Experienced

Can the old files in the ‘moveChunk’ directory be removed?

Yes, these files are made as backups during normal Shard balancing operations. Once the operations are done then they can be deleted. The clean-up process is currently manual so this needs to be taken care of to free up space.

What happens when a Shard is down or slow when querying?

If a Shard is down, the query will return an error unless the ‘Partial’ query options is set. If a shard is responding slowly, Mongos will wait for it.

What happens when a document is updated on a chunk that is being migrated?

The update will go through immediately on the old Shard and then the change will be replicated to the new Shard before ownership transfers.

When will data be on more than one Shard?

MongoDB Sharding is range-based. So all the objects in a collection lie into a chunk. Only when there is more than 1 chunk there is an option for multiple Shards to get data. Right now, the default chunk size is 64mb, so you need at least 64mb for migration.

What is the importance of GridFS and Journaling?

1>> GridFS: We use GridFS to retrieve and store large files like images, videos, and audio files.
2>> Journaling: We use Journaling for secure backups in MongoDB.

Does MongoDB write to disk automatically or slowly?

MongoDB writes data to disk in a haphazard fashion. It changes the data that is automatically written to the server, but it writes the data from the journal to disk slowly.

What have capped collections?

Capped collections are fixed-size collection and insert and retrieve data based on insertion order. If a collection’s space is full, the oldest records will be overwritten by the new documents in the collection.

How does concurrency affect primary replica set?

When the collection changes are written to primary, MongoDB writes the same to a special collection in the local database, called the primary’s oplog. Hence, both the collection’s database and local database are locked.

State the difference between find() and limit() method.

find() – displays only selected data rather than all the data of a document. For example, if your document has 4 fields but you want to show only one, set the required field as 1 and others as 0.

limit() – limit function limits the number of records fetched. For example, if you have 7 documents but want to display only the first 4 documents in a collection, use limit. Syntax –


Explain aggregation in MongoDB.

Aggregation groups values from different documents and performs operations on the data to return a single result. There are 3 ways in which MongoDB performs aggregation –

Aggregation pipeline
Map-reduce function
Single purpose aggregation methods

What are MongoDB’s data models?

The structure of documents influences data modeling. Related data in MongoDB may be embedded in a single document structure (embedded data model). Through references from one document to another, the relationship between data is stored. It is called the normalized data model.

How do you see the Mongos connection?

To see the connection used by Mongodb, use db adminCommand (“connPoolStats”);

On a high level, compare SQL databases and MongoDB.

SQL databases store data in the form of tables, rows, columns and documents. This data is contained in a pre-defined data format, which is not quite scalable for today’s rapidly increasing real-world applications. MongoDB, on the other hand, employs a modular framework that can be quickly changed and expanded.

How does MongoDB handle transactions and locks?

MongoDB uses multi-granularity locking wherein operations can be locked at the global, database or collection level. It is up to the storage engines to implement the level of concurrency. For example, in WiredTiger, it is at document-level. For reads, there is a shared locking mode, while for write there is an exclusive locking mode

Explain the role of profiler in MongoDB.

A profiler is an in-built tool that finds out the slow and resource-intensive queries and provides query-level insights. You as a dba can analyze the queries. Profiler stores all the data in system.profile collection.

What is a replica set?

We can specify the replica as a set of the mongo instances which host a similar data set. In the replica set, one node will be primary, and another one will be secondary. We replicate all the data from the primary to the secondary nodes.

What are the similarities and differences between sharding and replication in MongoDB?

Both sharding and replication require the use of several instances to host the database. Replicas are MongoDB instances that contain identical data, hence the name. To maximize redundancy and availability, we use replicas. In contrast, for sharding, each shard instance has data that is distinct from its neighbors. For horizontal scaling, we use sharding.

What are the various languages that MongoDB supports?

MongoDB officially supports the following languages: C, C++, C#, Java, Node.js, Perl, PHP, Python, Ruby, Scala, Go and Erlang. MongoDB can be used for any of the languages mentioned above. There are several other community-supported drivers, but the ones mentioned above are given by MongoDB.

What is an ACID transaction? Does MongoDB support it?

ACID stands for Atomicity, Consistency, Isolation, and Durability. The transaction manager ensures these attributes are taken care of. Yes, MongoDB version 4.0 supports ACID.

Explain the significance of the covered query?

A covered query makes the query implementation quicker as we store the indexes in the RAM or consecutively located on the disk. It makes query execution quicker. The covered query covers all the fields in the index, MongoDB matches the query condition along with returning the result fields.

Why MongoDB is the best NoSQL database?

MongoDB is the best NoSQL database due to the following features:

>> High Performance
>> High Availability
>> Easily Scalable
>> Rich Query Language
>> Document Oriented

Explain about Indexes in MongoDB?

In MongoDB, we use Indexes for executing the queries efficiently; without using Indexes, MongoDB should carry out a collection scan, i.e., scan all the documents of a collection, for selecting the documents which match the query statement. If a suitable index is available for a query, MongoDB will use an index for restricting the number of documents it should examine.

What’s a Master or Primary?

This is a node/member which is currently the primary and processes all writes for the replica set. During a failover event in a replica set, a different member can become primary.

How long does replica set failover take?

It may take 10-30 seconds for the primary to be declared down by the other members and a new primary to be elected. During this window of time, the cluster is down for primary operations i.e writes and strong consistent reads. However, eventually consistent queries may be executed to secondaries at any time (in slaveOk mode), including during this window.

Why are data files so large?

MongoDB does aggressive preallocation of reserved space to avoid file system fragmentation.

How do I do transactions/locking?

MongoDB does not use traditional locking or complex transactions with rollback, as it is designed to be light weight, fast and predictable in its performance. It can be thought of how analogous is to the MySQL’s MyISAM autocommit model. By keeping transaction support extremely simple, performance is enhanced, especially in a system that may run across many servers.

Does an update fsync to disk immediately?

No. Writes to disk are lazy by default. A write may only hit the disk a couple of seconds later. For example, if the database receives thousand increments to an object within one second, it will only be flushed to disk once. (Note: fsync options are available both at the command line and via getLastError_old.)

In what ways is MongoDB better than MySQL? Explain.

MongoDB has a flexible data model wherein the schema can be expanded based on business needs. Also, MongoDB is faster and can handle more data types to manage real-time applications better.

How are sharding and replication similar to each other in MongoDB, and how are they different?

Both sharding and replication involve using more than one instance to host the database.

Replicas are MongoDB instances with the same data, hence the name. We use replicas to increase redundancy and availability.

With sharding, on the other hand, each shard instance has different data from its neighbors. We use sharding for horizontal scaling.

Name the 2 storage engines using MongoDB?

MMAPv1 and WiredTiger are the 2 storage engines used by MongoDB.

What is a covered query and why is it important?

In a covered query, all the fields used in the query have the index created. The results returned should also be part of the index. Due to this, MongoDB fetches the results without actually looking inside documents, thus saving time and increasing efficiency.

What is sharding in MongoDB?

MongoDB meets the data growth using Sharding, which means sorting data records across various machines. Each shard is a replica set.

Name the default index created for a new collection.

Default index created for the new collection is _id

How can you query real-time MongoDB data?

Using the utilities mentioned can help handle live MongoDB data.

MongoHub has been migrated to a native Mac version.
This comes in handy with tree and document views.
Genghisapp – this is a web-based GUI that is clean, light-weight, easy to use, has keyboard shortcuts and performs fantastically well. GridFS is also supported.

What is BSON?

BSON or binary JSON is the binary encoded format of JSON. It extends JSON and provides more data types and fields.

What is ObjectId? How is it structured?

ObjectId is a class which is the default primary key for the document in MongoDB. It is found in the _id field of the inserted document. It is a 12-byte BSON type generated using a default algorithm. The structure is –
A 4-byte value that represents seconds since Unix epoch
3-byte machine id
2-byte process id
3-bytes counter that starts with a random number

How are constraints managed in MongoDB?

You can add document validator on the collections starting MongoDB 3.2. Unique indexes can also be created using db.collection.createIndex({“key” : 1} , );

How do you perform CRUD operations in MongoDB?

<> C – Create – db.collection.insert();
<> R – Read – db.collection.find();
<> U – Update – db.collection.update();
<> D – Delete – db.collection.remove({“fieldname” : ”value”});

MongoDB is called a schema-less database. Is it true? How to create the schema in MongoDB?

It would be more appropriate to say that MongoDB has dynamically typed schema because it relies on JSON which is a schema-free data structure. To create a schema, create and insert a document. Once a document is inserted, a corresponding collection will be created in the database.

How is data stored in MongoDB?

Since MongoDB is document-based, data is stored in BSON or Binary JavaScript Object Notation, which is the binary-encoded format of JSON.

What are 32-bit nuances?

With journaling, there is an additional memory-mapped file activity. This would also restrict the restricted database size of 32-bit builds. Journaling is now disabled by default on 32-bit computers.

What specifically is a collection in MongoDB?

A group of documents is referred to as a collection. If a document is the MongoDB analog of a row in a relational database, then a collection is the MongoDB analog of a table.

Dynamic schemas exist in collections. This indicates that the documents included inside a single collection may be of any shape. It’s worth noting that the previous documents’ values are not just of different types (string and integer), but they often have completely different keys. Any document can be added to any collection in MongoDB.

Differentiate MongoDB and MySQL?

Despite MySQL and MongoDB are freeware and open source databases, there are several differences between them in terms of a data relationship, transaction, performance speed, querying data, schema design, normalization, etc. Comparison between MongoDB and MySQL is similar to the comparison between Non-relational and Relational databases.

What type of NoSQL database MongoDB is?

MongoDB is a document-oriented database. It stores the data in the form of the BSON structure-oriented databases. We store these documents in a collection.

What is a storage engine? What are the different types of storage engines available in MongoDB?

The storage engine is the database part in charge of handling how data is processed, both in memory and on disk. MongoDB accepts many storage engines since various engines are best suited to different workloads.

<> The default storage engine is the WiredTiger storage engine. It is suitable for the majority of workloads and is recommended for modern implementations. WiredTiger includes, among other things, a document-level concurrency model, checkpointing and compression.
<> MongoDB Enterprise has an in-memory storage engine. It keeps records in memory rather than on disks for more predictable storage latencies.
<> MMAPv1 storage engine is discontinued as of MongoDB v4.0, but it was the default storage engine for MongoDB versions 3.0 and earlier.

What type of NoSQL database is MongoDB?

It is a document database that contains documents having key-value pairs, key-array pairs, and nested documents.

How does MongoDB store data?

Being document-based, MongoDB stores documents in BSON or Binary JavaScript Object notation which is the binary encoded format of JSON.

What is Mongo shell?

Mongo shell is a JavaScript interface to MongoDB that can be used to query and update data. It is interactive and can also be used to do administrative operations.

What are the features of MongoDB?

Following are the important features of MongoDB:

1. A compliant data model in the format of documents.
2. Agile and extremely scalable database.
3. Quicker than traditional databases.
4. Demonstrative query language.

What is MongoDB?

MongoDB is a cross-platform document-based database. Categorized as a NoSQL database, MongoDB avoids the conventional table-oriented relational database structure in support of the JSON-like documents with the dynamic schemas, making the data integration in specific kinds of applications quicker and simpler.

MongoDB was developed by a software company “10gen”, in October 2007 as an element of the planned platform as the service product. After that, the company was shifted to a freeware deployment model in 2009, providing sales assistance and other services.

R4R Team
R4R provides MongoDB Freshers questions and answers (MongoDB Interview Questions and Answers) .The questions on website is done by expert team! Mock Tests and Practice Papers for prepare yourself.. Mock Tests, Practice Papers,Interview question of mongoDB set 1,MongoDB Freshers & Experienced Interview Questions and Answers,MongoDB Objetive choice questions and answers,MongoDB Multiple choice questions and answers,MongoDB objective, MongoDB questions , MongoDB answers,MongoDB MCQs questions and answers Java, C ,C++, ASP, C# ,Struts ,Questions & Answer, Struts2, Ajax, Hibernate, Swing ,JSP , Servlet, J2EE ,Core Java ,Stping, VC++, HTML, DHTML, JAVASCRIPT, VB ,CSS, interview ,questions, and answers, for,experienced, and fresher R4r provides Python,General knowledge(GK),Computer,PHP,SQL,Java,JSP,Android,CSS,Hibernate,Servlets,Spring etc Interview tips for Freshers and Experienced for MongoDB fresher interview questions ,MongoDB Experienced interview questions,MongoDB fresher interview questions and answers ,MongoDB Experienced interview questions and answers,tricky MongoDB queries for interview pdf,complex MongoDB for practice with answers,MongoDB for practice with answers You can search job and get offer latters by studing .learn in easy ways .