Pages

January 27, 2020

#MongoDB Part 5: Interview Questions And Answers

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 is BSON?
BSON or binary JSON is the binary encoded format of JSON. It extends JSON and provides more data types and fields.

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 are the different index types in MongoDB?
  • Default, this is the _id that MongoDB creates
  • Single field for indexing and sorting on a single field
  • Compound for multiple fields
  • Multi-key for indexing array data
  • Hashed, indexes the hashes of a field value
  • Geospatial to query geospatial(location) data
What is the command to list all the indexes in a collection?
db.collection.getIndexes();

How are constraints managed in MongoDB?
We can add document validator on the collections starting MongoDB 3.2. Unique indexes can also be created using db.collection.createIndex({“key” : 1} , );

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.

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

What is journaling and how does it work?
MongoDB ensures data integrity by using an on-disk journal that is created for every write. In case of a server crash, the journal can be used to track the writes that were not written to the disk or data files.

How can you store images, videos and other large files in MongoDB?
Large files are stored in MongoDB using the GridFS specification.

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.

What is the default interval to write updates to the disk?
60 seconds.

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 and Single purpose aggregation methods

What have capped collections?
Capped collections are fixed-size collection, 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.

List some of the data types supported by MongoDB.
Some data types are Numbers, String, Array, Binary data, Boolean, Date, Regular expressions, ObjectId, etc.

How can applications access real-time data changes in MongoDB?
Applications can access real-time data changes using Change streams which acts as a subscriber to all the collection operations like insert, delete and update.

What is mongo in MongoDB?
mongo is an interactive JavaScript shell interface to MongoDB. It provides a powerful interface for system administrators as well as a way for developers to test queries and operations directly with the database.

What is mongod in MongoDB?
mongod is the MongoDB database server. The mongod process starts the MongoDB server as a daemon. The MongoDB server manages data requests and formats and manages background operations

What is mongos in MongoDB?
The routing and load balancing process that acts an interface between an application and a MongoDB sharded cluster.

Does MongoDB support primary-key and foreign-key relationship?
By Default, MongoDB doesn't support primary key-foreign key relationship. But, In MonogDB We can achieve primary key-foreign key relationship via embedding or referencing.

What is pipeline in MongoDB?
A series of operations in an aggregation process is called pipeline. The MongoDB aggregation pipeline consists of stages, each and every stage transforms the documents as they pass through the pipeline. Pipeline stages do not need to produce output documents for each and every input documents.

Can one MongoDB operation lock more than one databases? If yes, how?
Operations like copyDatabase(), repairDatabase(), etc. can lock more than one databases involved.

What is a Storage Engine in MongoDB?
A storage engine is the part of a database that is responsible for managing how data is stored on disk.

Are null values allowed in MongoDB?
Yes, but only for the members of an object. A null cannot be added to the database collection as it isn't an object. But {} can be added.

-K Himaanshu Shuklaa..

No comments:

Post a Comment