Interview Questions for MongoDB,
PostgreSQL, and Hibernate
This document contains a comprehensive set of interview questions covering MongoDB,
PostgreSQL, and Hibernate. Each question is explained with clear, concise, and detailed
concepts.
MongoDB Interview Questions
Basics
What is MongoDB and how is it different from traditional relational databases?
MongoDB is a NoSQL database that stores data in a flexible JSON-like format called BSON
(Binary JSON). Unlike relational databases that use structured tables and rows, MongoDB
allows for a more dynamic schema, meaning you can have different fields in different
documents within the same collection.
What are BSON and its advantages?
BSON (Binary JSON) is a format used to represent data in MongoDB. It supports more data
types than JSON, such as dates and binary data. Its advantages include efficient data storage
and faster data retrieval due to its binary nature.
What is a collection in MongoDB?
A collection in MongoDB is similar to a table in relational databases. It is a group of related
documents stored in the database. Each document in a collection can have different fields
and structures.
How do you define a schema in MongoDB?
In MongoDB, schemas are not strictly enforced, but you can define one using Mongoose (a
library for MongoDB) or by following conventions. You can set rules about the data types
and required fields to help maintain data consistency.
What is a document in MongoDB?
A document is a basic unit of data in MongoDB represented in BSON format. It is similar to a
row in a relational database and can contain various fields and values.
CRUD Operations
How do you insert a document into a MongoDB collection?
You can insert a document using the insertOne() or insertMany() methods. Example:
db.collectionName.insertOne({ name: "John", age: 30 });
What is the difference between insertOne() and insertMany()?
insertOne() is used to insert a single document, while insertMany() is used to insert
multiple documents at once. Example:
db.collectionName.insertMany([{ name: "Jane" }, { name: "Doe" }]);
How do you update a document in MongoDB?
You can update documents using the updateOne() or updateMany() methods. Example:
db.collectionName.updateOne({ name: "John" }, { $set: { age: 31 } });
What is the difference between update() and save()?
update() modifies existing documents based on a query, while save() is used to either insert
a new document or update an existing one. If the document already exists, save() updates it;
if not, it creates a new one.
How do you delete a document in MongoDB?
You can delete a document using deleteOne() or deleteMany() methods. Example:
db.collectionName.deleteOne({ name: "John" });
Querying Data
How do you retrieve a single document from a collection?
You can use the findOne() method to retrieve a single document. Example:
db.collectionName.findOne({ name: "Jane" });
What are query operators in MongoDB?
Query operators are special keywords used to specify conditions in your queries. Examples
include $eq (equal), $gt (greater than), $lt (less than), and $in (matches any value in an
array).
How do you perform a case-insensitive search?
You can perform a case-insensitive search using a regular expression. Example:
db.collectionName.find({ name: { $regex: /^john$/i } });
What is the purpose of the $exists operator?
The $exists operator checks if a specific field exists in a document. Example:
db.collectionName.find({ age: { $exists: true } });
Explain how to use the $or and $and operators in queries.
The $or operator allows you to specify multiple conditions where at least one must be true,
while $and requires all conditions to be true. Example:
db.collectionName.find({ $or: [{ age: { $lt: 25 } }, { name: "John" }] });