0% found this document useful (0 votes)
13 views

MongoDB Operations Guide

Uploaded by

shreyatyrtrtrtrt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

MongoDB Operations Guide

Uploaded by

shreyatyrtrtrtrt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

MongoDB Operations Guide

Perform MongoDB operations for the following:

i) Create a collection by name "Employee" having Empid, Firstname, Lastname, Emailid, and Salay
are the attributes.

ii) Display all the customers with their First name.

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.

Here are the MongoDB operations for the requested tasks:

i) Create a collection by name "Employee"

To create the `Employee` collection and insert some example documents:

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 }
]);

ii) Display all the employees with their First name

To retrieve and display only the `Firstname` of all employees:

javascript

db.Employee.find({}, { Firstname: 1, _id: 0 });

Explanation:

`{}`: Matches all documents.

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

db.Employee.find({ Salary: { $gt: 5000 } }).sort({ Salary: -1 });

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

db.Employee.find().sort({ Firstname: 1, Salary: -1 });

Explanation:

`.sort({ Firstname: 1, Salary: -1 })`: Sorts the results by ascending `Firstname` (1) and
descending `Salary` (-1).

Let me know if you need further assistance with MongoDB queries!

Perform MongoDB operations for the following:

i) Create a collection by name "Transaction" having Tid, CustomerName, ProductName, Price,


Quantity, Date as the fields.

ii) Insert 10 documents into the collection "Transaction"

iii)Find the last record in a table of daily transactions.

iv) Find the number of items bought by each customer.

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/9
Here are the MongoDB operations for the requested tasks:

i) Create a collection by name "Transaction"

To create the `Transaction` collection:

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") }
]);

ii) Insert 10 documents into the collection "Transaction"

The `insertMany()` operation above inserts 10 documents. Adjust the data as needed.

iii) Find the last record in a table of daily transactions

To find the last record in the `Transaction` collection based on the `Date` field:

javascript

db.Transaction.find().sort({ Date: -1 }).limit(1);

Explanation:

`.sort({ Date: -1 })`: Sorts documents in descending order by the `Date` field.
`.limit(1)`: Limits the result to the first document (latest record).

iv) Find the number of items bought by each customer

To calculate the total quantity of items bought by each customer:

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:

`$group`:Groups the documents by the `CustomerName` field.


`{ $sum: "$Quantity" }`: Sums up the `Quantity` for each customer.
`_id: "$CustomerName"`: Sets the `CustomerName` as the group identifier.

Let me know if you need further assistance with MongoDB queries!

Perform MongoDB operations for the following:

i) Create a collection by name "Transaction" having Tid, CustomerName, ProductName, Price,


Quantity, Date as the fields.

ii) Insert 10 documents into the collection "Transaction"

iii)Find the last record in a table of daily transactions.

iv) Find the number of items bought by each customer.

Here are the MongoDB operations for the requested tasks:

i) Create a collection by name "Transaction"

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") }
]);

ii) Insert 10 documents into the collection "Transaction"

The `insertMany()` command above inserts 10 documents into the `Transaction` collection.

iii) Find the last record in a table of daily transactions

To find the most recent record in the `Transaction` collection based on the `Date` field:

javascript

db.Transaction.find().sort({ Date: -1 }).limit(1);

Explanation:

`.sort({ Date: -1 })`: Sorts the documents in descending order of `Date`.


`.limit(1)`: Retrieves only the last record (most recent transaction).

iv) Find the number of items bought by each customer

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:

`$group`:Groups the documents by the `CustomerName` field.


`{ $sum: "$Quantity" }`: Adds up the `Quantity` of items for each customer.
`_id: "$CustomerName"`: Represents each unique customer name.

Let me know if you need further clarifications or additional MongoDB queries!

Perform MongoDB operations for the following:

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.

v) Write a query to sort the books in ascending order by ISBN no.

Here are the MongoDB operations for the requested tasks:

i) Create a collection by name "Books" and insert 5 documents

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

db.Books.find({ PublicationYear: 2015 }, { BookName: 1, Author: 1, ISBN: 1, _id: 0 });

Explanation:

`{ PublicationYear: 2015 }`: Filters books published in 2015.


`{ BookName: 1, Author: 1, ISBN: 1, _id: 0 }`: Displays only `BookName`, `Author`, and `ISBN`
fields, excluding the `_id`.

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:

`countDocuments()`: Returns the total number of documents in the `Books` collection.

v) Write a query to sort the books in ascending order by ISBN no.

javascript

db.Books.find().sort({ ISBN: 1 });

Explanation:

`.sort({ ISBN: 1 })`: Sorts the documents in ascending order of `ISBN`.

Let me know if you need further assistance!

Perform the following CURD operations for Inventory management system with the attributes (item,
qty, size, status)

i) Create Inventory collection and Insert 5 different items to the collection.

ii) Display first 3 items from the collection.

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

Here are the MongoDB operations for the requested tasks:

i) Create `Inventory` collection and Insert 5 different items to 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" }
]);

ii) Display first 3 items from the collection

javascript

db.Inventory.find().limit(3);

Explanation:

`.find()`:Retrieves all documents in the collection.


`.limit(3)`: Limits the result to the first 3 documents.

iii) Retrieve all documents from the inventory collection where status equals either
"A" or "D"

javascript

db.Inventory.find({ status: { $in: ["A", "D"] } });

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

db.Inventory.find({ status: "A", qty: { $lt: 30 } });

Explanation:

`status: "A"`:Filters documents where `status` equals `"A"`.


`qty: { $lt: 30 }`: Further filters where `qty` is less than 30.

v) Retrieve the item with the highest quantity along with its status from the
collection

javascript

db.Inventory.find().sort({ qty: -1 }).limit(1).project({ item: 1, status: 1, _id: 0 });

Explanation:

`.sort({ qty: -1 })`: Sorts the documents by `qty` in descending order.


`.limit(1)`: Retrieves only the document with the highest `qty`.
`.project({ item: 1, status: 1, _id: 0 })`: Displays only the `item` and `status` fields, excluding
`_id`.

Let me know if you need further clarifications or assistance!

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 9/9

You might also like