Lab Sheet 9
Lab Sheet 9
Why NoSQL?
● Scalability: NoSQL databases can scale out horizontally, making them ideal for
applications that require high throughput and large volumes of data.
● Flexibility: They allow for a more flexible and dynamic schema. This is particularly
beneficial for applications that evolve rapidly, as changes can be made without affecting
the entire database.
● Performance: Optimized for specific data models, NoSQL databases can offer improved
performance for certain types of applications, such as those requiring real-time analytics
or full-text search.
"age": 30,
"address": {
"city": "Anytown"
},
In this example, the document represents a user with basic information, an embedded document
(address), and an array of hobbies. This structure is highly readable and easy to work with, as it
closely resembles data structures used in modern programming languages.
.
Check the box to accept the terms and conditions.
Select the complete setup type.
Check the box to install mongoDB Compass (UI for mongodb).
After successful installation, open MondoDB Compass. The figure below shows the interface of
MongoDB Compass.
Click on connect, to connect to the local host.
Using the + icon in the left panel, we have a new database named ‘demo’ and collection named
‘student’. Please try it yourself and see how it works.
Now, you will have to edit the environment variable, by adding the path of mongodb compass.
The path is as follows: C:\Program Files\MongoDB\Server\7.0\bin.
Objective
This section focuses on designing a schema for a simplified social media platform and
implementing CRUD (Create, Read, Update, Delete) operations. By simulating the fundamental
functionalities of social media—user profiles, posts, and comments—you'll gain practical
experience with MongoDB's document model and operations.
Users Collection
The users collection will store user profiles, including basic information and an array of posts
and comments made by the user. Here's an example schema:
{
"_id": ObjectId("uniqueUserId"),
"username": "user_name",
"email": "[email protected]",
"posts": [
ObjectId("postId1"),
ObjectId("postId2")
],
"comments": [
ObjectId("commentId1"),
ObjectId("commentId2")
Posts Collection
The posts collection represents the messages or content users share. Each post is linked to a user
and can have multiple comments.
{
"_id": ObjectId("uniquePostId"),
"user": ObjectId("userId"),
"timestamp": ISODate("2023-01-01T00:00:00Z"),
"comments": [
ObjectId("commentId1"),
ObjectId("commentId2")
Comments Collection
Comments made on posts are stored in the comments collection. Each comment is associated
with a user and a post.
{
"_id": ObjectId("uniqueCommentId"),
"post": ObjectId("postId"),
"user": ObjectId("userId"),
"timestamp": ISODate("2023-01-02T00:00:00Z")
CRUD
CRUD operations represent the essential interactions with a database, encompassing the creation,
reading, updating, and deletion of data. In the context of NoSQL databases, which can include
document, key-value, wide-column, and graph databases, the specific syntax and operations can
vary depending on the type of NoSQL database you're using. Here, I'll provide a brief overview
focusing on MongoDB, a popular document-oriented NoSQL database, to illustrate CRUD
operations with syntax examples.
This updates the age of one document where the name is Alice.
This increments the age by 1 for all documents where the age is greater than 30.
This deletes all documents where the age is less than 30.
1. Retrieve comments made between February 1st, 2024 and February 28th, 2024.
2. Retrieve all posts made by a specific user by username. Hint: First, find the user's _id by
their username, then query the posts collection using the user field.
3. Add a new comment. Hint: Insert a new document into the comments collection with the
post field set to the post's _id.
5. Delete all comments made by a specific user from the comments collection, based on the
user's _id