21. Mention what is the basic syntax to use index in MongoDB?  

The basic syntax to use in MongoDB is >db.COLLECTION_NAME.ensureIndex ( {KEY:1} ). In here the key is the name of the file on which you want to create an index, where 1 is for ascending order while you use -1 for descending order.

22. Explain what are indexes in MongoDB?  

Indexes are special structures in MongoDB, which stores a small portion of the data set in an easy to traverse form. Ordered by the value of the field specified in the index, the index stores the value of a specific field or set of fields.

23. Which command can be used to provide various information on the query plans used by a MongoDB query?  

The explain() command can be used for this information. The possible modes are: 'queryPlanner', 'executionStats', and 'allPlansExecution'.

24. Define MongoDB?  

Short Answer

It is document oriented database which is used to high availability, easy scalability and high performance. It supports the dynamic schema design.

Long Answer

MongoDB is a cross-platform and open-source document-oriented database, a kind of NoSQL database. As a NoSQL database, MongoDB shuns the relational database’s table-based structure to adapt JSON-like documents that have dynamic schemas which it calls BSON.

This makes data integration for certain types of applications faster and easier. MongoDB is built for scalability, high availability and performance from a single server deployment to large and complex multi-site infrastructures.

25. What is the use of MongoDB?  

Short Answer

MongoDB is a document-oriented database. Instead of storing your data in tables made out of individual rows, like a relational database does, it stores your data in collections made out of individual documents. In MongoDB, a document is a big JSON blob with no particular format or schema.

Long Answer

MongoDB is relational database management system (RDBMS) replacement for web applications. So when you have something which is close to RDBMS, MongoDB could be of good use. It gives you that additional partition tolerance which RDMBS doesn't give but it has problems with availability. But if you want more scalability, MongoDB would be your choice. It's suitable for real-time analytics and high speed logging. It's highly scalable as well. Craigslist uses MongoDB for archived posts.

  • MongoDB is typically used as the primary data store for operational applications with real-time requirements (i.e., low-latency, high availability). MongoDB is generally a good fit for 60%-80% of the applications you may be building today. MongoDB is easy to operate and scale in ways that are hard if not impossible with relational databases.

  • MongoDB excels in many use cases where relational databases aren't a good fit, like applications with unstructured, semi-structured and polymorphic data, as well as applications with large scalability requirements or multi-data center deployments.

  • MongoDB may not be a good fit for some applications. For example, applications that require complex transactions (e.g., a double-entry bookkeeping system) and scan-oriented applications that access large subsets of the data most of the time may not be a good fit for MongoDB. MongoDB is not a drop-in replacement for legacy applications built around the relational data model and SQL.

  • Some common use cases include mobile apps, product catalogs, real-time personalization, content management and applications delivering a single view across multiple systems.

26. What kind of database is MongoDB?  

Short Answer

MongoDB is an open source database that uses a document-oriented data model. MongoDB is one of several database types to arise in the mid-2000s under the NoSQL banner. Instead of using tables and rows as in relational databases, MongoDB is built on an architecture of collections and documents.

Long Answer

MongoDB is a document-oriented DBMS. Think of MySQL but with JSON-like objects comprising the data model, rather than RDBMS tables. Significantly, MongoDB supports neither joins nor transactions. However, it features secondary indexes, an expressive query language, atomic writes on a per-document level, and fully-consistent reads.

Operationally, MongoDB features master-slave replication with automated failover and built-in horizontal scaling via automated range-based partitioning.

27. How does Journaling work in MongoDB?  

Short Answer

Write operations are saved in the memory while journaling is going on. The on-disk journal files are really dependable for the reason that the journal writes are habitual. Inside dbPath, a journal subdirectory is designed by MongoDB.

Long Answer

Here, once a commit happens, the same operation is replayed in shared view and then, after sixty seconds, the flash disc happens.

After it is flashed, the data is processed. The data here is marked as processed in the journal directory, which means that every sixty seconds, it checks the data it has copied and those that are supposed to be removed from journal.

Using Journaling is like using a log, the reason being, it creates a write operation log to increase the durability. Journaling is temporary storage, which means it keeps only write operation log as pending in journal directory. Also, the shared view has the data but journal directory has the operations.

For example, if the user is writing some data without Journaling, then whatever data is written, its memory mapping lets the user know the location where the data is written.

28. What is the use journaling?  

Short Answer

Journaling is used to safe backups in mongodb.

Long Answer

In this process, a write operation occurs in mongod, which then creates changes in private view. The first block is memory and the second block is ‘my disc’. After a specified interval, which is called a ‘journal commit interval’, the private view writes those operations in journal directory (residing in the disc).

Once the journal commit happens, mongod pushes data into shared view. As part of the process, it gets written to actual data directory from the shared view (as this process happens in background). The basic advantage is, we have a reduced cycle from 60 seconds to 200 milliseconds.

In a scenario where an abruption occurs at any point of time or flash disc remains unavailable for last 59 seconds (keeping in mind the existing data in journal directory/write operations), then when the next time mongod starts, it basically replays all write operation logs and writes into the actual data directory.

29. Which are the most important features of MongoDB?  

  • Flexible data model in form of documents.
  • Agile and highly scalable database.
  • Faster than traditional databases.
  • Expressive query language.

30. How is MongoDB better than other SQL databases?  

Short Answer

MongoDB allows a highly flexible and scalable document structure. For e.g. one data document in MongoDB can have five columns and the other one in the same collection can have ten columns. Also, MongoDB database are faster as compared to SQL databases due to efficient indexing and storage techniques.

Long Answer

Organizations of all sizes are adopting MongoDB because it enables them to build applications faster, handle highly diverse data types, and manage applications more efficiently at scale.

Development is simplified as MongoDB documents map naturally to modern, object-oriented programming languages. Using MongoDB removes the complex object-relational mapping (ORM) layer that translates objects in code to relational tables.

MongoDB’s flexible data model also means that your database schema can evolve with business requirements. For example, schema changes that took days of weeks in The Weather Channel's MySQL databases could be made in just hours with MongoDB.

MongoDB can also be scaled within and across multiple distributed data centers, providing new levels of availability and scalability previously unachievable with relational databases like MySQL. As your deployments grow in terms of data volume and throughput, MongoDB scales easily with no downtime, and without changing your application. In contrast, to achieve scale with MySQL often requires significant, custom engineering work. Baidu migrated from MySQL to MongoDB to support its rapidly growing business. The Chinese internet services giant now powers over 100 apps and manages in excess of 1PB of data with its MongoDB cluster.

31. Does MongoDB support ACID transaction management and locking functionalities?  

No, MongoDB does not support default multi-document ACID transactions. However, MongoDB provides atomic operation on a single document.

32. Does MongoDB support foreign key constraints?  

No, MongoDB does not support such relationships.

33. How can you achieve primary key-foreign key relationships in MongoDB?  

By default MongoDB does not support such primary key - foreign key relationships. However, we can achieve this concept by embedding one document inside another. Foe e.g. an address document can be embedded inside customer document.

34. Does MongoDB pushes the writes to disk immediately or lazily?  

MongoDB pushes the data to disk lazily. It updates the immediately written to the journal but writing the data from journal to disk happens lazily.

35. Explain the structure of ObjectID in MongoDB?  

ObjectID is a 12-byte BSON type with:
  • 4 bytes value representing seconds.
  • 3 byte machine identifier.
  • 2 byte process id.
  • 3 byte counter.

36. Mention the command to insert a document in a database called school and collection called persons?  

db.products.insert( { item: "card", qty: 15 } )

37. How many indexes does MongoDB create by default for a new collection?  

By default, MongoDB created the _id collection for every collection.

38. Can you create an index on an array field in MongoDB? If yes, what happens in this case?  

Yes, An array field can be indexed in MongoDB. In this case, MongoDB would index each value of the array.

39. What is a covered query in MongoDB?  

Short Answer

As per the official MongoDB documentation, a covered query is a query in which −
  • All the fields in the query are part of an index.
  • All the fields returned in the query are in the same index.

Long Answer

Since all the fields present in the query are part of an index, MongoDB matches the query conditions and returns the result using the same index without actually looking inside the documents. Since indexes are present in RAM, fetching data from indexes is much faster as compared to fetching data by scanning documents.

40. Why is a covered query important?  

Since all 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. Since indexes are stored in RAM or sequentially located on disk, such access is a lot faster.

.Net Interview Question

PHP Interview Question

Java Interview Question

AngularJS Interview Questions