MongoDB Operations Guide
MongoDB Operations Guide
i) Create a collection by name "Employee" having Empid, Firstname, Lastname, Emailid, and Salay
are the attributes.
iii) Find where employee salary > 5000 and sort the result by descending order of salary.
iv) Write a query to sort list by ascending order of firstName and descending order of salary.
javascript
db.Employee.insertMany([
{ Empid: 1, Firstname: "John", Lastname: "Doe", Emailid: "[email protected]", Salary:
6000 },
{ Empid: 2, Firstname: "Jane", Lastname: "Smith", Emailid: "[email protected]", Sal
ary: 4500 },
{ Empid: 3, Firstname: "Alice", Lastname: "Brown", Emailid: "[email protected]", S
alary: 7000 },
{ Empid: 4, Firstname: "Bob", Lastname: "Johnson", Emailid: "[email protected]", S
alary: 3000 }
]);
javascript
Explanation:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/9
`{ Firstname: 1, _id: 0 }`: Includes only the `Firstname` field and excludes the `_id` field from
the result.
iii) Find where employee salary > 5000 and sort the result by descending order of
salary
To query employees with a `Salary` greater than 5000 and sort them in descending order of salary:
javascript
Explanation:
`{ Salary: { $gt: 5000 } }`: Filters documents where `Salary` is greater than 5000.
`.sort({ Salary: -1 })`: Sorts the result in descending order of the `Salary`.
iv) Write a query to sort the list by ascending order of `Firstname` and descending
order of `Salary`
To sort the employees by ascending order of `Firstname` and descending order of `Salary`:
javascript
Explanation:
`.sort({ Firstname: 1, Salary: -1 })`: Sorts the results by ascending `Firstname` (1) and
descending `Salary` (-1).
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/9
Here are the MongoDB operations for the requested tasks:
javascript
db.Transaction.insertMany([
{ Tid: 1, CustomerName: "Alice", ProductName: "Laptop", Price: 1200, Quantity: 1, Date:
new Date("2024-12-08") },
{ Tid: 2, CustomerName: "Bob", ProductName: "Phone", Price: 800, Quantity: 2, Date: new
Date("2024-12-07") },
{ Tid: 3, CustomerName: "Charlie", ProductName: "Tablet", Price: 300, Quantity: 1, Date:
new Date("2024-12-08") },
{ Tid: 4, CustomerName: "Alice", ProductName: "Monitor", Price: 200, Quantity: 2, Date:
new Date("2024-12-06") },
{ Tid: 5, CustomerName: "Bob", ProductName: "Keyboard", Price: 50, Quantity: 3, Date: ne
w Date("2024-12-06") },
{ Tid: 6, CustomerName: "David", ProductName: "Mouse", Price: 30, Quantity: 1, Date: new
Date("2024-12-05") },
{ Tid: 7, CustomerName: "Alice", ProductName: "Headphones", Price: 100, Quantity: 1, Dat
e: new Date("2024-12-08") },
{ Tid: 8, CustomerName: "Eve", ProductName: "Charger", Price: 20, Quantity: 2, Date: new
Date("2024-12-07") },
{ Tid: 9, CustomerName: "Bob", ProductName: "USB Cable", Price: 10, Quantity: 4, Date: n
ew Date("2024-12-06") },
{ Tid: 10, CustomerName: "Charlie", ProductName: "Power Bank", Price: 40, Quantity: 1, D
ate: new Date("2024-12-05") }
]);
The `insertMany()` operation above inserts 10 documents. Adjust the data as needed.
To find the last record in the `Transaction` collection based on the `Date` field:
javascript
Explanation:
`.sort({ Date: -1 })`: Sorts documents in descending order by the `Date` field.
`.limit(1)`: Limits the result to the first document (latest record).
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 3/9
javascript
db.Transaction.aggregate([
{ $group: { _id: "$CustomerName", TotalItems: { $sum: "$Quantity" } } }
]);
Explanation:
To create the `Transaction` collection, you can use the following command:
javascript
db.Transaction.insertMany([
{ Tid: 1, CustomerName: "Alice", ProductName: "Laptop", Price: 1200, Quantity: 1, Date:
new Date("2024-12-08") },
{ Tid: 2, CustomerName: "Bob", ProductName: "Phone", Price: 800, Quantity: 2, Date: new
Date("2024-12-07") },
{ Tid: 3, CustomerName: "Charlie", ProductName: "Tablet", Price: 300, Quantity: 1, Date:
new Date("2024-12-08") },
{ Tid: 4, CustomerName: "Alice", ProductName: "Monitor", Price: 200, Quantity: 2, Date:
new Date("2024-12-06") },
{ Tid: 5, CustomerName: "Bob", ProductName: "Keyboard", Price: 50, Quantity: 3, Date: ne
w Date("2024-12-06") },
{ Tid: 6, CustomerName: "David", ProductName: "Mouse", Price: 30, Quantity: 1, Date: new
Date("2024-12-05") },
{ Tid: 7, CustomerName: "Alice", ProductName: "Headphones", Price: 100, Quantity: 1, Dat
e: new Date("2024-12-08") },
{ Tid: 8, CustomerName: "Eve", ProductName: "Charger", Price: 20, Quantity: 2, Date: new
Date("2024-12-07") },
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 4/9
{ Tid: 9, CustomerName: "Bob", ProductName: "USB Cable", Price: 10, Quantity: 4, Date: n
ew Date("2024-12-06") },
{ Tid: 10, CustomerName: "Charlie", ProductName: "Power Bank", Price: 40, Quantity: 1, D
ate: new Date("2024-12-05") }
]);
The `insertMany()` command above inserts 10 documents into the `Transaction` collection.
To find the most recent record in the `Transaction` collection based on the `Date` field:
javascript
Explanation:
To calculate the total quantity of items bought by each customer, use the aggregation framework:
javascript
db.Transaction.aggregate([
{ $group: { _id: "$CustomerName", TotalItems: { $sum: "$Quantity" } } }
]);
Explanation:
i)Create a collection by name "Books" and insert 5 documents having Book Name, Author,
Publication year, ISBN no., Edition as the fields.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 5/9
ii) Display Book Name, Author, ISBN no. of all the "Books" with the Publication Year is 2015.
iii)Write a query to search for all the books with the same author name.
iv) Write a query to count the total number books in the collection.
javascript
db.Books.insertMany([
{ BookName: "Book A", Author: "Author 1", PublicationYear: 2015, ISBN: "12345", Edition:
"1st" },
{ BookName: "Book B", Author: "Author 2", PublicationYear: 2018, ISBN: "23456", Edition:
"2nd" },
{ BookName: "Book C", Author: "Author 1", PublicationYear: 2015, ISBN: "34567", Edition:
"3rd" },
{ BookName: "Book D", Author: "Author 3", PublicationYear: 2020, ISBN: "45678", Edition:
"1st" },
{ BookName: "Book E", Author: "Author 2", PublicationYear: 2015, ISBN: "56789", Edition:
"1st" }
]);
ii) Display Book Name, Author, ISBN no. of all the "Books" with the Publication Year
2015
javascript
Explanation:
iii) Write a query to search for all the books with the same author name
javascript
db.Books.aggregate([
{
$group: {
_id: "$Author",
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 6/9
Books: { $push: "$BookName" },
Count: { $sum: 1 }
}
},
{ $match: { Count: { $gt: 1 } } }
]);
Explanation:
`$group`: Groups documents by `Author` and collects all their book names using `$push`.
`{ $match: { Count: { $gt: 1 } } }`: Filters only authors with more than one book.
iv) Write a query to count the total number of books in the collection
javascript
db.Books.countDocuments();
Explanation:
javascript
Explanation:
Perform the following CURD operations for Inventory management system with the attributes (item,
qty, size, status)
iii) Retrieve all documents from the inventory collection where status equals either " A"
or "D":
iv)Retrieve all documents in the inventory collection the status equals " A" and qty is less
than ($lt) 30 where
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 7/9
v) Retrieve the item with highest quantity with its status from the collection
javascript
db.Inventory.insertMany([
{ item: "item1", qty: 25, size: "small", status: "A" },
{ item: "item2", qty: 50, size: "medium", status: "B" },
{ item: "item3", qty: 15, size: "large", status: "A" },
{ item: "item4", qty: 40, size: "medium", status: "D" },
{ item: "item5", qty: 10, size: "small", status: "A" }
]);
javascript
db.Inventory.find().limit(3);
Explanation:
iii) Retrieve all documents from the inventory collection where status equals either
"A" or "D"
javascript
Explanation:
`{ $in: ["A", "D"] }`: Filters documents where `status` matches either `"A"` or `"D"`.
iv) Retrieve all documents in the inventory collection where status equals "A" and
qty is less than 30
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 8/9
javascript
Explanation:
v) Retrieve the item with the highest quantity along with its status from the
collection
javascript
Explanation:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 9/9