0% found this document useful (0 votes)
3 views10 pages

Node.js and MongoDB Interview Guide

Uploaded by

sabir0657
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Node.js and MongoDB Interview Guide

Uploaded by

sabir0657
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Instructions

Read the highlighted points carefully

These points may be especially useful for answering interview questions related to [Link].
They often cover key concepts, definitions, use cases, and differences that interviewers are
likely to focus on. Make sure you understand not just what each feature does, but also why
it is used and how it benefits real-world applications.

Start With Use Cases First


As highlighted in the document, ensure you go through real-life and technical use cases of
[Link] before reading or presenting its internal workings.

Use Paragraphs for Flow, Points for Clarity


When you are clear, especially in explanations, use bullet points. When summarizing a
process, use paragraph format.
4. Introduction to MongoDB and NoSQL Databases
1. What is MongoDB
MongoDB is a NoSQL (Non-Relational), document-oriented database designed to store,
retrieve, and manage large volumes of data that may not have a fixed structure.
Unlike traditional databases that store data in rows and columns, MongoDB stores
information in the form of documents inside collections.
A document is similar to a JSON object (JavaScript Object Notation) and can contain nested
fields, arrays, and other complex data types.
MongoDB is highly flexible and allows dynamic changes to the data structure without
redefining schemas like in relational databases.
Key Features
• Schema-less: Each document can have a different structure.
• Scalable: Can handle large volumes of unstructured data easily.
• High performance: Fast read/write operations.
• Flexible data model: You can store arrays, nested objects, etc.
Why NoSQL?
Traditional SQL databases are great for structured data, but they struggle when:
• Data is unstructured or semi-structured.
• The data volume grows rapidly.
• The schema changes frequently.
• There is a need for faster and distributed storage.
MongoDB and other NoSQL databases were created to overcome these issues.

SQL Vs NoSQL
Feature SQL (Relational DB) NoSQL (MongoDB)

Data Structure Tables (rows, columns) Documents (JSON-like)

Schema Fixed Flexible

Relationships Uses Joins Embedded or referenced

Scalability Vertical (add more power) Horizontal (add more servers)


Types of NoSQL Databases
1. Document-based: (MongoDB, CouchDB) - store data as JSON-like documents.
2. Key-Value Stores: (Redis, DynamoDB) - store data as key-value pairs.
3. Column-based: (Cassandra, HBase) - optimized for read-heavy workloads.
4. Graph-based: (Neo4j) - designed for data with relationships (like social networks).
Use Cases
• E-commerce: Product catalogs with varying attributes.
• Social media: User posts, comments, and profiles (dynamic data).
• IoT Systems: Device data storage where structure changes frequently.
• Content management: Blogs or news sites with different types of content.
2. CRUD Operations in MongoDB
CRUD stands for Create, Read, Update, Delete - the four main database operations.
MongoDB provides simple and flexible commands to perform these operations on
documents.
2.1 Create (Insert Data)
Used to add new documents to a collection.
Commands:
• insertOne() - Inserts one document.
• insertMany() - Inserts multiple documents.

This command creates a new document in the students collection with the given data.

2.2 Read (Retrieve Data)


Used to fetch documents from the collection.
Commands:
• find() - Retrieves all or filtered documents.
• findOne() - Retrieves a single document.

This will return all students enrolled in the “MCA” course.


2.3 Update (Modify Data)
Used to change existing data.
Commands:
• updateOne() – Updates a single matching document.
• updateMany() – Updates multiple documents.

This modifies the year of the student named "Ravi" to 2026.

2.4 Delete (Remove Data)


Used to delete one or more documents.
Commands:
• deleteOne() – Removes one document.
• deleteMany() – Removes all matching documents.

This deletes all documents where the course is “MBA”.


Use Cases
• Create: Add new student or product records.
• Read: Fetch data to display on the website or app.
• Update: Change details like phone number or order status.
• Delete: Remove old or inactive users.
3. Data Modelling Best Practices
Data modelling means organizing data efficiently inside MongoDB.
Since MongoDB does not enforce a strict schema, developers must plan how to represent
data effectively.
3.1 Documents and Collections
• Document: A record in MongoDB (like a row in SQL).
• Collection: A group of related documents (like a table in SQL).

3.2 Embedding vs Referencing


• Embedding: Store related data in the same document.
o Good for small, closely related data.

• Referencing: Store related data in different collections using references.


o Good for large or frequently reused data.

3.3 Indexing
Indexing helps MongoDB to locate data faster, just like an index in a book.
Without indexes, MongoDB scans the entire collection.
3.4 Schema Validation
Although MongoDB is schema-less, we can define validation rules to ensure data
consistency.

Concised Points:
• Embed data when relationships are simple.
• Use references for large or complex relationships.
• Create indexes on frequently searched fields.
• Avoid deeply nested documents.
• Keep documents size small (recommended < 16 MB).
4. Mongoose and ODM Basics
4.1 What is Mongoose
Mongoose is an ODM (Object Data Modelling) library for [Link].
It acts as a bridge between [Link] and MongoDB.
It allows you to define schemas (data structure) and models (blueprints for documents).
4.2 Advantages of Using Mongoose
• Simplifies database operations.
• Provides built-in validation and middleware.
• Handles relationships easily.
• Prevents syntax errors and structure mistakes.
4.3 Core Concepts
1. Schema: Defines the structure of documents.
2. Model: Used to create and query documents.
3. Document: Actual record stored in the database.

Now Student is a model representing the students collection.


4.4 CRUD using Mongoose
Insert:

Read:

Update:

Delete:

For Example:
In a college management system, Mongoose helps define models for Students, Teachers,
Courses, and Attendance.
Whenever the frontend sends a request, [Link] interacts with MongoDB through
Mongoose to fetch or update the required data.
5. Database Connectivity with [Link]
5.1 Overview
To perform operations on MongoDB from a [Link] application, we must establish a
database connection.
[Link] connects to MongoDB either:
• Using the native MongoDB driver, or
• Using Mongoose, which simplifies the process.
5.2 Steps to Connect Using Mongoose

• Install Mongoose
• Connect to MongoDB
• Define Schema and Model
• Perform CRUD Operations

Example:
Suppose you’re building a web application for student registration.
• Frontend (HTML/React): Student fills out the registration form.
• Backend ([Link] + Express): Receives the form data and processes it.
• Database (MongoDB): Stores the student’s data permanently.
Whenever the admin checks the student list, [Link] queries MongoDB and displays the
data on a webpage.

You might also like