MongoDB Basic Interview Questions

Introduction

This guide covers fundamental MongoDB interview questions that are commonly asked in technical interviews. Each question includes a detailed answer and relevant code examples.

Easy

1. What is MongoDB?

MongoDB is a popular NoSQL database that stores data in flexible, JSON-like documents. It's designed for scalability, performance, and developer productivity.

Key Features:
  • Document-oriented storage
  • Dynamic schema
  • Horizontal scalability
  • High performance
  • Rich query language
Easy

2. What are the main components of MongoDB?

MongoDB consists of several key components:

  • Mongod: The primary daemon process for MongoDB
  • Mongos: The routing service for sharded clusters
  • Mongo Shell: Interactive JavaScript interface
  • MongoDB Compass: GUI for database management
  • MongoDB Tools: Utilities for database administration
Easy

3. What is a Document in MongoDB?

A document in MongoDB is a data structure composed of field and value pairs. Documents are similar to JSON objects but use BSON format.

// Example Document
{
    "_id": ObjectId("507f1f77bcf86cd799439011"),
    "name": "John Doe",
    "age": 30,
    "email": "john@example.com",
    "address": {
        "street": "123 Main St",
        "city": "New York",
        "zip": "10001"
    },
    "hobbies": ["reading", "gaming", "coding"]
}
Easy

4. What is a Collection in MongoDB?

A collection is a group of MongoDB documents, similar to a table in relational databases. Collections don't enforce a schema, allowing documents within a collection to have different fields.

// Creating a collection
db.createCollection("users")

// Inserting documents into collection
db.users.insertOne({
    name: "John Doe",
    email: "john@example.com"
})

// Viewing collection documents
db.users.find()
Medium

5. What is the difference between MongoDB and SQL databases?

MongoDB SQL Databases
Document-oriented Table-oriented
Dynamic schema Fixed schema
Horizontal scaling Vertical scaling
JSON-like documents Rows and columns
No joins required Uses joins
Medium

6. What is the purpose of _id in MongoDB?

The _id field is a unique identifier for each document in a MongoDB collection. It has the following characteristics:

  • Automatically generated if not specified
  • 12-byte ObjectId by default
  • Guaranteed to be unique within a collection
  • Indexed by default
// Example of _id field
{
    "_id": ObjectId("507f1f77bcf86cd799439011"),
    "name": "John Doe"
}

// Custom _id
{
    "_id": "user123",
    "name": "John Doe"
}
Medium

7. What are the different types of NoSQL databases?

NoSQL databases are categorized into four main types:

  • Document Databases: MongoDB, CouchDB
  • Key-Value Stores: Redis, DynamoDB
  • Column-Family Stores: Cassandra, HBase
  • Graph Databases: Neo4j, OrientDB
MongoDB's Position:

MongoDB is a document database that stores data in BSON format, offering flexibility and scalability for modern applications.

Medium

8. What is BSON in MongoDB?

BSON (Binary JSON) is a binary-encoded serialization of JSON-like documents used by MongoDB. It provides:

  • Efficient storage and retrieval
  • Support for additional data types
  • Faster parsing and serialization
  • Better performance than JSON
// JSON
{
    "name": "John",
    "age": 30,
    "date": "2023-01-01"
}

// BSON (conceptual)
{
    "name": "John",
    "age": 30,
    "date": ISODate("2023-01-01")
}

Next Steps

Continue your MongoDB interview preparation with: