LAB RECORD NoSql
LAB RECORD NoSql
require("dotenv").config();
const express = require("express");
const redis = require("redis");
const bodyParser = require("body-parser");
// Connect to Redis
const redisClient = redis.createClient({
url: process.env.REDIS_URL || "redis://localhost:6379",
});
redisClient.connect()
.then(() => console.log("Connected to Redis"))
.catch((err) => console.log("Redis Connection Error:", err));
res.json(JSON.parse(student));
} catch (err) {
res.status(500).json({ error: err.message });
}
});
Output
1. POST /students
• Input (Request Body): When you send a POST request to /students with the following
JSON body (for example):
json
CopyEdit
{
"rollNumber": "101",
"name": "John Doe",
"class": "10th Grade",
"email": "[email protected]",
"phone": "1234567890"
}
• Process:
o The server checks if the rollNumber is provided. If it is, it proceeds to save the
student data.
o The student details are stored in Redis under the key student:101.
• Output: If the request is successful, the server will respond with:
json
CopyEdit
{
"message": "Student added successfully!"
}
• If the rollNumber is missing in the request body, it will respond with an error:
json
CopyEdit
{
"error": "Roll Number is required"
}
2. GET /students/:rollNumber
• Input (Request): When you send a GET request to /students/101 (assuming you have
already added a student with roll number 101 using the POST endpoint), the server will
fetch the data stored in Redis.
• Process:
o The server will try to get the student data using the key student:101.
o If the student data exists, it will return the details in JSON format.
• Output: The response will be:
json
CopyEdit
{
"name": "John Doe",
"studentClass": "10th Grade",
"email": "[email protected]",
"phone": "1234567890"
}
• If no student is found for the given rollNumber, it will return:
json
CopyEdit
{
"message": "Student not found"
}
3. Error Handling
• If there's any error while connecting to Redis or during the execution of any operation,
such as an invalid request or server failure, the server will respond with a 500 status
code and the corresponding error message.
Example of running the program:
1. Start the server with node <your_file_name.js>.
2. Make a POST request to add student details:
o Using tools like Postman or curl:
bash
CopyEdit
curl -X POST http://localhost:5000/students -H "Content-Type: application/json" -d
'{"rollNumber": "101", "name": "John Doe", "class": "10th Grade", "email":
"[email protected]", "phone": "1234567890"}'
o You will get:
json
CopyEdit
{
"message": "Student added successfully!"
}
3. Make a GET request to retrieve student details:
bash
CopyEdit
curl http://localhost:5000/students/101
o You will get:
json
CopyEdit
{
"name": "John Doe",
"studentClass": "10th Grade",
"email": "[email protected]",
"phone": "1234567890"
}
If there is an issue connecting to Redis or any error occurs in the code, you'll get a 500 error
response with the respective message.
Apache Cassandra is an open-source database management system that runs on Not Only
SQL model and uses a partitioned wide column storage data model. It mainly focuses on speed,
scalability, and performance.
First, access your VPS via SSH and determine if you have Java installed.
java -version
Cqlsh
cqlsh, or Cassandra query language shell, is used to communicate with Cassandra and initiate
Cassandra Query Language. To start cqlsh, use the following command/
root@myawesomevps:/# cqlsh
CREATE KEYSPACE:
A keyspace specifies data replication. In the following example, we will create a new keyspace
and specify the replication factor:
cqlsh> CREATE KEYSPACE testingout
WITH REPLICATION = {
'class' : 'SimpleStrategy',
'replication_factor' : 1
};
SHOW
The SHOW command displays all the information about the current cqlsh session. You can
choose between showing host, version, and session information:
USE
The USE command sets the current working keyspace:
CREATE TABLE
In order to create a table, users need to use the CREATE TABLE command.
cqlsh:testingout>
INSERT INTO tabletest (name, surname, phone)
VALUES ('John', 'Johnson', 456123789);
Procedure:
To install MongoDB in an Ubuntu environment, you can follow these steps:
db.student.find()
db.students.find({"regNo":"3014"})
db.student.update({
"regNo": "3014"
},
$set:
{
"name":"Viraj"
})
db.collection_name.remove({"fieldname":"value"})
db.collection_name.remove({"regno":"3014"})
<html>
<head>
<title>Add tasks</title>
</head>
<body>
<form method="post" action="/">
<input name = "inputvalue" type = "text" placeholder="Enter a task"> <button> Submit</button>
<button id="view"> <a href="/tasks"> View Tasks </a></button> </form>
</body>
</html>
INDEX.JS file
TASKS.EJS file
<!DOCTYPE html>
<html>
<head>
<title>My Array</title>
</head>
<body>
<h1>Your To-Do List</h1>
<ul>
<% tasks.forEach(function(item) { %>
<li><%= item.task %></li>
<% }); %>
</ul>
</body>
</html>
require("dotenv").config();
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
// MongoDB Connection
mongoose
.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("MongoDB Connected"))
.catch((err) => console.log("DB Connection Error:", err));
// Student Schema
const studentSchema = new mongoose.Schema({
name: String,
rollNumber: { type: String, unique: true, required: true },
class: String,
email: String,
phone: String,
});
// Routes
// Add Student
app.post("/students", async (req, res) => {
try {
const student = new Student(req.body);
await student.save();
res.status(201).json({ message: "Student added successfully!", student });
} catch (err) {
res.status(400).json({ error: err.message });
}
});
Graph database is a database used to model the data in the form of graph. In here, the nodes of
a graph depict the entities while the relationships depict the association of these nodes.
By default, Neo4j will be accessible on http://localhost:7474. You can access the Neo4j browser
by opening that URL in a web browser.
During the installation process, you'll be prompted to set up a password for the neo4j user. You
can check the status of the Neo4j service using the following command:
The graph shows how we use a simple linked list of shopping baskets connected by NEXT
relationships to create a purchase history for the customer.
Explanation:
In the graph above, we see that the customer has visited three times, saved their first purchase
for later (the SAVED relationship between customer and basket nodes).
Ultimately, the customer bought one basket (indicated by the BOUGHT relationship between
customer and basket node) and is currently assembling a basket, shown by the CURRENT
relationship that points to an active basket at the head of the linked list.
Conclusion: In graph form, it’s easy to figure out the customer’s behavior: They became a
(potential) new customer but failed to commit to buying toothpaste and came back one day later
and bought toothpaste, bread and butter. Finally, the customer settled on buying bread and
butter in their next purchase – which is a repeated pattern in their purchase history we could
ultimately use to serve them better.
Creating Relationship
A) Create a group of four friends, mark their relationship as friends and then
create another node name "Vishnu institute of technology" join these people as
"students" in Vishnu institute of technology.
F) Delete the Bob, Gaming nodes and the relationship from the database and print
the final nodes and relationships
MATCH (s:Student)-[:HAS_HOBBY]->(h:Hobby)
RETURN s.name AS Student, h.name AS Hobby;
MATCH (s:Student)-[:FRIEND]->(f:Student)
RETURN s.name AS Student, f.name AS Friend;
D) View All Nodes and Relationships
MATCH (n)
RETURN n;
MATCH (n)-[r]->(m)
RETURN n, r, m;
E) Change the Music Hobby to "Movies"
MATCH (n)-[r]->(m)
RETURN n, r, m;