5.
1 Study of Open Source NOSQL Database and installation of
MongoDB
Different GUIs used for MongoDB - MongoDB Compass, Studio 3T,
NoSQLBooster for MongoDB, Robo 3T (formerly Robomongo),
Mongoclient (github) etc.
5.2 Create, drop, rename the database in MongoDB
use niet
show dbs
Create a new database and copy collections:
use niet
Insert into collection
[Link]({ name: "Alice", age: 22, city: "New York" })
Rename the collection
[Link]().forEach(doc=>[Link](doc))
use new_database
db.old_collection.aggregate([{ $out: "new_collection" }])
Drop Database
[Link]()
Find a document in collection
[Link]({})
5.3 Implementation the MongoDB Operators.
Comparison Operators
[Link]({ age: { $gt: 18 } }) // Find students older than 18
[Link]({ name: { $in: ["Alice", "raj"] } }) // Find students
named Alice or raj
Logical Operators
[Link]({ $and: [{ age: { $gt: 18 } }, { city: "New
York" }] }) // Students older than 18 and from New York
[Link]({ $or: [{ age: { $lt: 18 } }, { city: "delhi" }] }) //
Students younger than 18 or from delhi
Element Operators
Used to check field existence or type
[Link]({ email: { $exists: true } }) // Find students with an
email field
[Link]({ age: { $type: "number" } }) // Find students where
age is a number
Array Operators
Used for querying array fields.
[Link]({ subjects: { $all: ["Math", "Science"] } }) //
Students who study both Math & Science
[Link]({ subjects: { $size: 3 } }) // Students enrolled in
exactly 3 subjects
[Link]({ marks: { $elemMatch: { $gt: 80 } } }) // Students
with at least one mark > 80
Update Operators
Used to modify existing documents.
[Link]({ name: "Alice" }, { $set: { age: 20 } }) //
Update Alice’s age to 20
[Link]({ name: "Bob" }, { $inc: { marks: 5 } }) //
Increase Bob’s marks by 5
[Link]({ name: "Alice" }, { $push: { subjects:
"Physics" } }) // Add Physics to Alice’s subjects
Aggregation Operators
Used for processing and transforming documents.
[Link]([
{ $group: { _id: "$city", avgAge: { $avg: "$age" } } } // Average
age of students per city
])
Text Search Operators
Used for text-based queries.
[Link]({ name: "text" }) // Create text index
[Link]({ $text: { $search: "Alice" } }) // Find documents
containing "Alice"
5.4 Implementation the CRUD Operation in MongoDB how do we
implement these commands
MongoDB provides operations for Create, Read, Update, and Delete
(CRUD) operations, which are essential for interacting with data.
Create Operation (Insert)
To add a document to a collection, you use the insertOne() or
insertMany() methods.
Insert a single document:
[Link]({ employeeID: 1, firstName: "John",
lastName: "Doe", department: "HR", salary: 60000 })
Insert multiple documents:
[Link]([
{ employeeID: 2, firstName: "Alice", lastName: "Smith",
department: "IT", salary: 70000 },
{ employeeID: 3, firstName: "Bob", lastName: "Johnson",
department: "Finance", salary: 75000 }
])
Read Operation (Query)
You can retrieve data from MongoDB using the find() method.
Find all employees:
[Link]()
Find employees with a specific salary:
[Link]({ salary: 70000 })
Find a single employee by employeeID:
[Link]({ employeeID: 1 })
Projection (Selecting Specific Fields): To return only certain fields of
a document, use the second argument to find().
[Link]({}, { firstName: 1, salary: 1 })
Update Operation (Modify)
You can update existing documents using updateOne(),
updateMany(), or replaceOne().
Update a single document (e.g., updating salary for employee with
employeeID: 1):
[Link]({ employeeID: 1 }, { $set: { salary: 75000 }
}
Update multiple documents (e.g., increasing the salary by 5000 for all
employees in the HR department):
[Link]({ department: "HR" }, { $inc: { salary:
5000 } })
Delete Operation
To remove documents, you can use the deleteOne() or deleteMany()
methods.
Delete a single document:
[Link]({ employeeID: 3 })
Delete multiple documents (e.g., delete all employees in the IT
department):
[Link]({ department: "IT" })