0% found this document useful (0 votes)
6 views7 pages

Chapter 4 Webx Ritesh

Chapter 4 discusses MongoDB, detailing its data types such as String, Number, Boolean, Array, Object, Object Id, and Date, all stored in BSON format. It explains CRUD operations for managing documents, the default databases (admin, local, config) created by MongoDB, and the concepts of collections and documents. Additionally, it highlights MongoDB's applications in various domains like content management systems, real-time analytics, e-commerce, and social media, along with an overview of REST APIs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Chapter 4 Webx Ritesh

Chapter 4 discusses MongoDB, detailing its data types such as String, Number, Boolean, Array, Object, Object Id, and Date, all stored in BSON format. It explains CRUD operations for managing documents, the default databases (admin, local, config) created by MongoDB, and the concepts of collections and documents. Additionally, it highlights MongoDB's applications in various domains like content management systems, real-time analytics, e-commerce, and social media, along with an overview of REST APIs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Chapter 4: MongoDB

Q1 Explain MongoDB data type along with syntax?


Ans: MongoDB stores data in BSON (Binary JSON) format. BSON supports a
wide range of data types to represent different kinds of values.
1. String
 Stores textual data.
 Most commonly used data type.
Ex- { "name": "Alice" }
2. Number
MongoDB supports two main types of numbers:
 Integer (32-bit or 64-bit depending on server architecture):
Ex { "age": 25 }
Double (Floating-point number):
Ex { "price": 12.99 }
3. Boolean
 Stores true or false.
Ex { "isActive": true }
4. Array
 Stores multiple values in a single field.
 Can include different data types (strings, numbers, documents, etc.).
Ex { "skills": ["JavaScript", "MongoDB", "Node.js"] }
5. Object / Embedded Document
 A document within another document (used for nested data).
 Syntax:
Ex {
"address": {
"city": "Mumbai",
"zip": 400001
}
}
6. Object Id
 A 12-byte unique identifier used as a primary key (_id).
 Automatically generated if not provided.
Ex { "_id": ObjectId("507f191e810c19729de860ea") }
7. Date
 Stores the date and time in UTC format.
Ex { "createdAt": new Date()}

Q2 Explain MongoDB CRUD operation with an example?


Ans: MongoDB supports four primary operations commonly known as CRUD:
C – Create
R – Read
U – Update
D – Delete
These operations allow us to manage documents inside MongoDB collections.
1. CREATE Operation
Used to insert new documents into a collection.
➤ Methods:
 insertOne() – inserts a single document
 insertMany() – inserts multiple documents
Ex db.users.insertOne({
name: "ritesh",
age: 20,
email: "[email protected]"
});
2. READ Operation
Used to retrieve data from the database.
➤ Methods:
 find() – fetches all matching documents
 findOne() – fetches a single matching document
Ex db.users.find({ name: "ritesh" });
3. UPDATE Operation
Used to modify existing documents.
➤ Methods:
 updateOne() – updates the first matching document
 updateMany() – updates all matching documents
Ex db.users.updateOne(
{ name: "ritesh" },
{ $set: { age: 21 } }
);
4. DELETE Operation
Used to remove documents from a collection.
➤ Methods:
 deleteOne() – deletes the first matching document
 deleteMany() – deletes all matching documents
Ex db.users.deleteOne({ name: "ritesh" });
Q3 Discuss default database in MongoDB: local, admin and config?
Ans: MongoDB creates three default databases automatically when installed.
These are system-level databases used by MongoDB to manage internal
operations and configurations.
1. admin Database
 The admin database is the root or superuser database in MongoDB.
 If a user is granted access to the admin database, they can access all other
databases too.
 It stores user authentication and role information.
Ex use admin
db.createUser({
user: "adminUser",
pwd: "securePass",
roles: ["root"]
});
2. local Database
 The local database is used for local operations specific to the server and
is not replicated in a replica set.
 It stores metadata like logs and replication info.
 Used by MongoDB internally for Storing oplog.rs (operation log for replication)

3. config Database
 The config database is used in sharded clusters.
 It stores all the metadata related to sharding and cluster configuration.
Contains collections like:
 databases – list of databases in the cluster
 collections – sharded collections
 chunks – data chunk mapping to shards
Ex use config
db.databases.find();
Q4. Explain the concept of collection and document in MongoDB with
example?
Ans: MongoDB is a NoSQL database that stores data in a flexible, JSON-like
format called BSON (Binary JSON). The two core building blocks of
MongoDB data structure are:
 Collections (equivalent to tables in relational databases)
 Documents (equivalent to rows or records)
Document in MongoDB
A Document is the basic unit of data in MongoDB. It is a set of key-value
pairs, similar to a JSON object.
 Documents are stored in BSON format.
 They can contain nested documents (documents within documents).
 Fields can store different data types: string, number, date, array, etc.
Ex {
"_id": 1,
"name": "Alice",
"age": 22,
"course": "Computer Science",
"subjects": ["DBMS", "OS", "MongoDB"]
}
Collection in MongoDB
A Collection is a group of documents. Think of it as similar to a table in SQL.
 A collection does not require a fixed schema, so documents inside it can
have different fields.
 Collections are created automatically when a document is inserted.
 We can create a collection by using db.createCollection("students").

Inserting data in collection


db.students.insertOne({
name: "ram",
age: 22,
course: "Computer Science"
});
Q5 Explain the working of MongoDB and application of MongoDB?
Ans: MongoDB is a NoSQL database that stores data in a flexible, JSON-like
format called BSON (Binary JSON) how it works:
Document-Oriented Storage:
 Data is stored in the form of documents (similar to JSON).
 Each document can have different fields, structures, and data types.
Ex {
"name": "Alice",
"age": 25,
"skills": ["JavaScript", "MongoDB"]
}
Collections Instead of Tables:
 Documents are grouped into collections (like tables in SQL).
 No fixed schema is required, allowing flexibility in storing data.
CRUD Operations:
 MongoDB supports basic operations:
o Create: Insert documents
o Read: Query documents
o Update: Modify documents
o Delete: Remove documents
Applications of MongoDB:
Content Management Systems (CMS):
MongoDB is ideal for CMS platforms like blogs, news websites, and e-
commerce systems due to its flexible schema.
Real-Time Analytics:
Used for tracking website clicks, user behavior, or sensor data in real time.
E-Commerce Platforms:
Stores product details, customer profiles, order histories, and payment
information efficiently
Social Media Applications:
Perfect for storing dynamic user content such as posts, likes, comments,
messages, and friend connections.

Q6. What does REST stand for? Explain REST API with basic flow diagram?
Ans: REST stands for Representational State Transfer.
A REST API (or RESTful API) is an interface that allows communication
between client and server using standard HTTP methods.
It allows CRUD (Create, Read, Update, Delete) operations on resources using:
GET, POST, PUT, DELETE methods.

Each HTTP method in a REST API serves a specific purpose:


 GET: Fetch data
 POST: Create new data
 PUT: Fully update existing data
 DELETE: Remove data
Ex Real-Life Example
When you use a mobile app to see weather data:
 It calls a REST API like GET https://api.weather.com/data?city=Mumbai
 The server responds with JSON data:
{ "city": "Mumbai", "temperature": "32°C" }

You might also like