0% found this document useful (0 votes)
9 views3 pages

Mswdfinalquiz

The document provides code to create a sample application that connects to a MongoDB Atlas database to maintain information about campus recruitment training. It defines schemas and models for a Student and JobOffer collection. Express routes are created to save new student and job offer documents to the collections and return the saved documents. On a successful database connection, it displays a message and provides routes to create and retrieve student and job offer records.

Uploaded by

kiralight511
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Mswdfinalquiz

The document provides code to create a sample application that connects to a MongoDB Atlas database to maintain information about campus recruitment training. It defines schemas and models for a Student and JobOffer collection. Express routes are created to save new student and job offer documents to the collections and return the saved documents. On a successful database connection, it displays a message and provides routes to create and retrieve student and job offer records.

Uploaded by

kiralight511
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Question text

Our K L University CSE department wishes to maintain a portal for monitoring Campus
Recruitment Training (CRT).
a. Create a sample application that will create the following 2 collections while
connecting the MongoDB Atlas. (Assume data as per your requirement)
i. Student: student_id, name, year, semester, specialization,
date, time, CRT_attendance, Job_id
ii. Job_offer: Job_id, Company_name, Date_of_campassing,
contact_no, email
b. After a successful connection, it will show the message “Database is connected” in
the console. Thereafter create two server routes for creating new student and Job_offer
and show their details. (Assume data as per your requirement).

Install the required packages using the following commands:


code npm init -y npm install express mongoose

const express = require('express');

const mongoose = require('mongoose');

const app = express();

const port = 3000;

// Replace 'YOUR_CONNECTION_STRING' with your MongoDB Atlas connection string

const mongoURI = 'YOUR_CONNECTION_STRING';

mongoose.connect(mongoURI, { useNewUrlParser: true, useUnifiedTopology: true });

const db = mongoose.connection;

db.on('error', console.error.bind(console, 'MongoDB connection error:'));

db.once('open', () => {

console.log('Database is connected');

// Define student schema


const studentSchema = new mongoose.Schema({

student_id: String,

name: String,

year: String,

semester: String,

specialization: String,

date: String,

time: String,

CRT_attendance: Boolean,

Job_id: String,

});

// Define job offer schema

const jobOfferSchema = new mongoose.Schema({

Job_id: String,

Company_name: String,

Date_of_campassing: String,

contact_no: String,

email: String,

});

// Create models

const Student = mongoose.model('Student', studentSchema);

const JobOffer = mongoose.model('JobOffer', jobOfferSchema);

// Express routes

app.use(express.json());

// Route to create a new student

app.post('/createStudent', async (req, res) => {

try {
const newStudent = new Student(req.body);

const savedStudent = await newStudent.save();

res.json(savedStudent);

} catch (error) {

res.status(500).json({ error: error.message });

});

// Route to create a new job offer

app.post('/createJobOffer', async (req, res) => {

try {

const newJobOffer = new JobOffer(req.body);

const savedJobOffer = await newJobOffer.save();

res.json(savedJobOffer);

} catch (error) {

res.status(500).json({ error: error.message });

});

// Start the server

app.listen(port, () => {

console.log(`Server is running on port ${port}`);

});

});

JUNNURI MOHAN KARTHIKEYA


2200031604

You might also like