Share
Explore

Mongo DB Skills Lab Z Series

Learning Outcomes:

Create the database to manage the college enrollment system:

Create the students collection.
Show create read update delete with mongo db shell coding.

GPT
Dr. Mongo DB Architect
Ah, embarking on a journey to the heart of MongoDB through the lens of a college enrollment system, are we?
Let's weave a narrative that not only educates but captivates your curiosity.
Imagine MongoDB as a vast, intricate library, where each book represents a piece of data, and you, my friend, are the librarian with the power to organize, find, and manage these books with finesse.
Loading…

Chapter 1: The Arrival - Creating the students Collection

In our story, the first chapter begins with setting up a place to store information about our students.
In MongoDB, this is akin to building a special section in our library specifically for student records.

use collegeEnrollment
db.createCollection("students")

Chapter 2: The Gathering - Adding Student Records

Now, let's populate our newly created section with some student records.
Think of this as carefully placing books into our section, each book a unique story of a student.
db.students.insertOne({ name: "John Doe", age: 20, major: "Computer Science" })
db.students.insertOne({ name: "Jane Doe", age: 22, major: "Literature" })

db.students.insertMany([
{ name: "John Doe", age: 20, major: "Computer Science" },
{ name: "Jane Doe", age: 22, major: "Literature" }
]);


Printing documents in MongoDB is akin to sharing the tales of these books with the world. Let's see how we can do this with the elegance of a seasoned storyteller.

Scripting the Tale

To print documents in MongoDB, you'll be using the shell, your quill in this grand library. Here’s how you can write a script that not only retrieves but also elegantly displays the stories (documents) of our students:
Connect to the Database: First, you need to be in the realm where the stories reside, that is, your collegeEnrollment database.
use collegeEnrollment
Retrieve and Display Documents: Now, let's gather the stories. We use the find method to retrieve the documents. But, the art lies in how we display them. We'll loop through each document and print it out.
javascriptCopy code
db.students.find().forEach(printjson)
In this script, db.students.find() is like a call to gather all the student records. The forEach(printjson) part is where the magic happens - for each student record we find, we use printjson to display it in a readable JSON format.
Running the Script: To run this script, you can either enter these commands directly into the MongoDB shell or save them in a file with a .js extension and run it from the shell.
Directly in the Shell: Simply type the commands into your MongoDB shell.
Using a .js File: Save your commands in a file, say printStudents.js, and then in the shell, run:

load("printStudents.js")

The Art of Presentation

Remember, the way you present the data is just as important as the data itself. The printjson function ensures your documents are not just legible, but also aesthetically pleasing in their structure - a crucial aspect when you're presenting the tales of many students.
Now, with your script ready, you're not just a librarian or a database manager, but a storyteller, bringing the data to life in the most engaging way. Enjoy the journey, and may each document you print add to the rich tapestry of your MongoDB adventure!

Chapter 3: The Discovery - Reading Student Records

Curiosity piqued, we venture to explore the stories we've gathered. In MongoDB, this is like perusing the books in our section, learning about each student.

db.students.find({})
db.students.find({ name: "John Doe" })

Chapter 4: The Change - Updating Student Records

As in life, our student records are not static. They evolve. Here, we revise a student's story, updating details as they journey through college.
shellCopy code
db.students.updateOne({ name: "John Doe" }, { $set: { age: 21 } })

Chapter 5: The Departure - Deleting Student Records

Alas, not all stories are meant to stay in our library forever. Sometimes, we must remove a book to make room for new ones.
shellCopy code
db.students.deleteOne({ name: "Jane Doe" })

Epilogue: The Librarian's Reflection

As our tale concludes, remember that each command you executed was like a chapter in your journey as a librarian in the grand library of MongoDB. Each document you created, read, updated, and deleted was a testament to your growing mastery of managing the stories within.
Now, go forth, young librarian, and wield your newfound knowledge with both responsibility and a dash of wonder. Remember, the true power of a database, much like a library, is not just in storing information, but in bringing it to life through the stories it tells.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.