MongoDB is an open-source NoSQL database written in C++ language. It uses JSON-like documents with optional schemas. The following are frequently asked MongoDB questions in interviews for freshers as well experienced developers:
1. What is NoSQL?
The internet is loaded with big data, big users, big complexity, etc. And also becoming more complex day by day. NoSQL is the answer to all these problems, It is not a traditional database management system, not even a relational database management system (RDBMS). NoSQL stands for “Not Only SQL”. NoSQL is a type of database that can handle and sort all types of unstructured, messy, and complicated data. It is just a new way to think about the database.
2 what is MongoDB?
- MongoDB is an open-source NoSQL database written in C++ language. It uses JSON-like documents with optional schemas.
- It provides easy scalability and is a cross-platform, document-oriented database.
- MongoDB works on the concept of Collection and Document.
- It combines the ability to scale out with features such as secondary indexes, range queries, sorting, aggregations, and geospatial indexes.
- MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License (SSPL).
3. What are some features of MongoDB?
- Support ad hoc queries
- Indexing
- Replication
- Duplication of data
- Load balancing
- Supports map reduction and aggregation tools.
- Uses JavaScript instead of Procedures.
- It is a schema-less database written in C++.
- Provides high performance.
- Stores files of any size easily without complicating your stack.
- Easy to administer in the case of failures.
4. What are the different types of NoSQL databases? Give some examples.
NoSQL databases can be classified into 4 basic types:
- Key-value store NoSQL database
- Document store NoSQL database
- Column store NoSQL database
- Graph base NoSQL database
5. What kind of NoSQL database MongoDB is?
MongoDB is a document database. Graph databases are used to store information about networks, such as social connections. Examples include Neo4J and HyperGraphDB. NoSQL databases are rising in popularity as companies apply them to a growing number of use cases.
6. How is MongoDB better than other SQL databases?
MongoDB is easy to use, robust, and scalable database.
- The data model is JSON documents that are easy to understand.
- The physical structure of databases, collections, and documents is again easy to understand and learn.
- Very good documentation.
- It is easy to install and administer.
- It supports server-side sharding which makes it horizontally scalable.
- It supports replication that makes the shards highly available.
- Drivers are available for most programming languages that enable integration with your applications.
7. Does MongoDB need a lot of RAM?
MongoDB requires approximately 1 GB of RAM per 100.000 assets. If the system has to start swapping memory to disk, this will have a severely negative impact on performance and should be avoided.
8. Which are the different languages supported by MongoDB?
MongoDB provides official driver support for C, C++, C#, Java, Node.js, Perl, PHP, Python, Ruby, Scala, Go, and Erlang.
You can use MongoDB with any of the above languages. There are some other community-supported drivers too but the above-mentioned ones are officially provided by MongoDB.
9. Is MongoDB better than other SQL databases? If yes then how?
MongoDB is better than other SQL databases because it allows a highly flexible and scalable document structure.
For example:
- One data document in MongoDB can have five columns and the other one in the same collection can have ten columns.
- MongoDB databases are faster than SQL databases due to efficient indexing and storage techniques.
10. Can you achieve primary key – foreign key relationships in MongoDB?
We can achieve a primary key-foreign key relationship by embedding one document inside another. For example An address document can be embedded inside a customer document.
11. When to use MongoDB?
You should use MongoDB when you are building internet and business applications that need to evolve quickly and scale elegantly. MongoDB is popular with developers of all kinds who are building scalable applications using agile methodologies.
MongoDB is a great choice if one needs to:
- Support a rapid iterative development.
- Scale to high levels of reading and write traffic – MongoDB supports horizontal scaling through Sharding, distributing data across several machines, and facilitating high throughput operations with large sets of data.
- Scale your data repository to a massive size.
- Evolve the type of deployment as the business changes.
- Store, manage and search data with text, geospatial, or time-series dimensions.
12. What language you can use with MongoDB?
MongoDB client drivers support all the popular programming languages so there is no issue with language, you can use any language that you want.
13. Does MongoDB database have tables for storing records?
No. Instead of tables, MongoDB uses “Collections” to store data.
14. Do the MongoDB databases have a schema?
Yes. MongoDB databases have a dynamic schema. There is no need to define the structure to create collections.
15. What is the method to configure the cache size in MongoDB?
MongoDB’s cache is not configurable. Actually, MongoDB uses all the free spaces on the system automatically by way of memory-mapped files.
16. How to do Transaction/locking in MongoDB?
MongoDB doesn’t use traditional locking or complex transaction with Rollback. MongoDB is designed to be light weighted, fast, and predictable in its performance. It keeps transaction support simply to enhance performance.
17. Why 32-bit versions of MongoDB are not preferred?
Because MongoDB uses memory-mapped files so when you run a 32-bit build of MongoDB, the total storage size of the server is 2 GB. But when you run a 64-bit build of MongoDB, this provides virtually unlimited storage size. So 64-bit is preferred over 32-bit.
18. Is it possible to remove old files in the moveChunk directory?
Yes, These files can be deleted once the operations are done because these files are made as backups during normal shard balancing operations. This is a manual cleanup process and is necessary to free up space.
19. What will have to do if a shard is down or slow and you do a query?
If a shard is down and you even do a query then your query will be returned with an error unless you set a partial query option. But if a shard is slow the Mongos will wait for them till response.
20. Explain the covered query in MongoDB.
A query is called a covered query if satisfies the following two conditions:
- The fields used in the query are part of an index used in the query.
- The fields returned in the results are in the same index.
21. What is the importance of a covered query?
A covered query makes the execution of the query faster because indexes are stored in RAM or sequentially located on disk. It makes the execution of the query faster.
A covered query makes the fields are covered in the index itself, MongoDB can match the query condition as well as return the result fields using the same index without looking inside the documents.
22. What is the difference between MongoDB and MySQL?
MongoDB | MySQL |
---|---|
MongoDB is an open-source database developed by MongoDB, Inc. MongoDB stores data in JSON-like documents that can vary in structure. It is a popular NoSQL database. | MySQL is a popular open-source relational database management system (RDBMS) that is developed, distributed, and supported by Oracle Corporation. |
In MongoDB, each individual record is stored as ‘documents’. | In MySQL, each individual record are stored as ‘rows’ in a table. |
Documents belonging to a particular class or group as stored in a ‘collection’. Example: a collection of users. | A ‘table’ is used to store rows (records) of a similar types. |
MongoDB was designed with high availability and scalability in mind and includes out-of-the-box replication and sharding. | MySQL concept does not allow efficient replication and sharding but in MySQL, one can access associated data using joins which minimizes duplication. |
Practical Question
23. What is the Mongo Shell?
It is a JavaScript shell that allows interaction with a MongoDB instance from the command line. With that one can perform administrative functions, inspect an instance, or explore MongoDB.
To start the shell, run the mongo executable:
$ mongod $ mongo MongoDB shell version: 4.2.0 connecting to: test >
The shell is a full-featured JavaScript interpreter, capable of running arbitrary JavaScript programs. Let’s see how basic math works on this:
> x = 100; 200 > x / 5; 20
24. How to add data in MongoDB?
The basic method for adding data to MongoDB is “inserts”. To insert a single document, use the collection’s insertOne
method:
> db.books.insertOne({"title" : "Start With Why"})
For inserting multiple documents into a collection, we use insertMany
. This method enables passing an array of documents to the database.
25. How do you Update a Document?
Once a document is stored in the database, it can be changed using one of several update methods: updateOne
, updateMany
, and replaceOne. updateOne
and updateMany
each takes a filter document as its first parameter and a modifier document, which describes changes to make, as the second parameter. replaceOne also takes a filter as the first parameter but as the second parameter replaceOne
expects a document with which it will replace the document matching the filter.
For example, in order to replace a document:
{ "_id" : ObjectId("4b2b9f67a1f631733d917a7a"), "name" : "alice", "friends" : 24, "enemies" : 2 }
26. How do you Delete a Document?
The CRUD API in MongoDB provides deleteOne
and deleteMany
for this purpose. Both of these methods take a filter document as their first parameter. The filter specifies a set of criteria to match against in removing documents.
> db.books.deleteOne({"_id" : 3})
27. How to perform queries in MongoDB?
The find
the method is used to perform queries in MongoDB. Querying returns a subset of documents in a collection, from no documents at all to the entire collection. Which documents get returned is determined by the first argument to find
, which is a document specifying the query criteria.
> db.users.find({"age" : 24})
28. What are the data types in MongoDB?
MongoDB supports a wide range of data types as values in documents. Documents in MongoDB are similar to objects in JavaScript. Along with JSON’s essential key/value–pair nature, MongoDB adds support for a number of additional data types. The common data types in MongoDB are:
Null{"x" : null}
Boolean{"x" : true}
Number{"x" : 4}
String{"x" : "foobar"}
Date{"x" : new Date()}
Regular expression{"x" : /foobar/i}
Array{"x" : ["a", "b", "c"]
} Embedded document{"x" : {"foo" : "bar"}}
Binary Data Binary data is a string of arbitrary bytes. Code{"x" : function() { /* ... */ }}