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

Practical 15: Write Code To Perform Insert, Find, Update, and Delete Operations On Student Database Using Node - Js and Mongodb

The document describes code to perform CRUD operations on a student database using Node.js and MongoDB. It includes code samples to insert documents into the student collection using insertOne() and insertMany(), find documents using various query parameters, and sort the results. The student documents have fields for enrollment number, name, semester, branch, mobile number, email, and address.

Uploaded by

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

Practical 15: Write Code To Perform Insert, Find, Update, and Delete Operations On Student Database Using Node - Js and Mongodb

The document describes code to perform CRUD operations on a student database using Node.js and MongoDB. It includes code samples to insert documents into the student collection using insertOne() and insertMany(), find documents using various query parameters, and sort the results. The student documents have fields for enrollment number, name, semester, branch, mobile number, email, and address.

Uploaded by

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

3161611- Advanced Web Programming | 2022-23 | Enrolment No: _______________

Practical 15: Write code to perform Insert, find, update, and delete operations on Student
database using Node.js and MongoDB.
Student database fields: Enrollment_No., Name, Semester, Branch, Mobile_No, Email, Address

Prerequisite:

(1) Mongo DB Database: https://www.mongodb.com/try/download/community

(2) Nodejs: https://nodejs.org/en/

(3) Mongo DB Driver: project-path> npm install mongodb

(4) Mongo DB Package: https://www.npmjs.com/package/mongodb

Solution:

insert.js

const { MongoClient } = require('mongodb');


const url = 'mongodb://127.0.0.1:27017';
const client = new MongoClient(url);
const dbName ='practiceDB';
async function main() {
await client.connect();
console.log('Connected successfully to server');
const db = client.db(dbName);
const collection = db.collection('students');

// insertOne() Operation Code


const insertResult = await collection.insertOne(
{
Enrollment_No: 6001,
Name: "Umang",
Semester: 6,
Branch: "IT",
Mobile_No:"9988776655",
Email: "[email protected]",
Address: "a-101 xyz street..."
});
console.log('Inserted documents =>', insertResult);

// insertMany() Operation Code


const insertResult2 = await collection.insertMany([
{
Enrollment_No: 6001,
Name: "Umang",
Semester: 6,
Branch: "IT",
Mobile_No:"9988776655",
Email: "[email protected]",
Address: "a-101 xyz street..."
},
6th Semester IT (SSEC, Bhavnagar) Page : 1
3161611- Advanced Web Programming | 2022-23 | Enrolment No: _______________
{
Enrollment_No: 6002,
Name: "Dhaval",
Semester: 6,
Branch: "IT",
Mobile_No:"9876543210",
Email: "[email protected]",
Address: "b-202 pqr colony"
},
{
Enrollment_No: 6003,
Name: "Ajay",
Semester: 6,
Branch: "IT",
Mobile_No:"9944332211",
Email: "[email protected]",
Address: "c-303 mno colony"
},
{
Enrollment_No: 6004,
Name: "Bansi",
Semester: 6,
Branch: "IT",
Mobile_No:"9876543210",
Email: "[email protected]",
Address: "x-401 abc colonywxyz colony"
},
{
Enrollment_No: 6005,
Name: "Binal",
Semester: 6,
Branch: "IT",
Mobile_No:"9876543210",
Email: "[email protected]",
Address: "x-101 abc colony"
}
]);
console.log('Inserted documents =>', insertResult2)
}

6th Semester IT (SSEC, Bhavnagar) Page : 2


3161611- Advanced Web Programming | 2022-23 | Enrolment No: _______________
Output:

PS D:\2023 EVEN\AWP\Lab Session\Practical 15> node insert.js


Connected successfully to server
Inserted documents => {
acknowledged: true,
insertedId: new ObjectId("640038ce471e8d22bffd5214")
}
Inserted documents => {
acknowledged: true,
insertedCount: 5,
insertedIds: {
'0': new ObjectId("640038ce471e8d22bffd5215"),
'1': new ObjectId("640038ce471e8d22bffd5216"),
'2': new ObjectId("640038ce471e8d22bffd5217"),
'3': new ObjectId("640038ce471e8d22bffd5218"),
'4': new ObjectId("640038ce471e8d22bffd5219")
}
}

find.js

const { MongoClient } = require('mongodb');


const url = 'mongodb://127.0.0.1:27017';
const client = new MongoClient(url);
const dbName ='practiceDB';
async function main() {
await client.connect();
console.log('Connected successfully to server');
const db = client.db(dbName);
const collection = db.collection('students');

// Read: find() Operation...


const findResult = await collection.find({}).toArray();
console.log('Found documents =>', findResult);

const queryResult1 = await collection.find({Enrollment_No:6001}).toArray();


console.log('Find document with Enrollment_No = 6001..... ');
console.log('Found documents =>', queryResult1);

const queryResult2 = await collection.find({Enrollment_No:{$in:[6001,6003]}}).toArray();


console.log('Find document in between Enrollment_No 6001 and 6003... ');
console.log('Found documents =>', queryResult2);

// Sort() Operation...
const sortResult = await collection.find({}).sort( {Enrollment_No:1}).toArray();
console.log('Sorted documents =>', sortResult);
}
main()
.then(console.log)

6th Semester IT (SSEC, Bhavnagar) Page : 3


3161611- Advanced Web Programming | 2022-23 | Enrolment No: _______________
.catch(console.error)
.finally(() => client.close());

Output:

PS D:\2023 EVEN\AWP\Lab Session\Practical 15> node find.js


Connected successfully to server
Found documents => [
{
_id: new ObjectId("640038ce471e8d22bffd5214"),
Enrollment_No: 6001,
Name: 'Umang',
Semester: 6,
Branch: 'IT',
Mobile_No: '9988776655',
Email: '[email protected]',
Address: 'a-101 xyz street...'
},
{
_id: new ObjectId("640038ce471e8d22bffd5215"),
Enrollment_No: 6001,
Name: 'Umang',
Semester: 6,
Branch: 'IT',
Mobile_No: '9988776655',
Email: '[email protected]',
Address: 'a-101 xyz street...'
},
{
_id: new ObjectId("640038ce471e8d22bffd5216"),
Enrollment_No: 6002,
Name: 'Dhaval',
Semester: 6,
Branch: 'IT',
Mobile_No: '9876543210',
Email: '[email protected]',
Address: 'b-202 pqr colony'
},
{
_id: new ObjectId("640038ce471e8d22bffd5217"),
Enrollment_No: 6003,
Name: 'Ajay',
Semester: 6,
Branch: 'IT',
Mobile_No: '9944332211',
Email: '[email protected]',
Address: 'c-303 mno colony'
},
{
_id: new ObjectId("640038ce471e8d22bffd5218"),
6th Semester IT (SSEC, Bhavnagar) Page : 4
3161611- Advanced Web Programming | 2022-23 | Enrolment No: _______________
Enrollment_No: 6004,
Name: 'Bansi',
Semester: 6,
Branch: 'IT',
Mobile_No: '9876543210',

Email: '[email protected]',
Address: 'x-401 abc colonywxyz colony'
},
{
_id: new ObjectId("640038ce471e8d22bffd5219"),
Enrollment_No: 6005,
Name: 'Binal',
Semester: 6,
Branch: 'IT',
Mobile_No: '9876543210',
Email: '[email protected]',
Address: 'x-101 abc colony'
}
]
Find document with Enrollment_No = 6001.....
Found documents => [
{
_id: new ObjectId("640038ce471e8d22bffd5214"),
Enrollment_No: 6001,
Name: 'Umang',
Semester: 6,
Branch: 'IT',
Mobile_No: '9988776655',
Email: '[email protected]',
Address: 'a-101 xyz street...'
},
{
_id: new ObjectId("640038ce471e8d22bffd5215"),
Enrollment_No: 6001,
Name: 'Umang',
Semester: 6,
Branch: 'IT',
Mobile_No: '9988776655',
Email: '[email protected]',
Address: 'a-101 xyz street...'
}
]
Find document in between Enrollment_No 6001 and 6003...
Found documents => [
{
_id: new ObjectId("640038ce471e8d22bffd5214"),
Enrollment_No: 6001,
Name: 'Umang',
Semester: 6,
6th Semester IT (SSEC, Bhavnagar) Page : 5
3161611- Advanced Web Programming | 2022-23 | Enrolment No: _______________
Branch: 'IT',
Mobile_No: '9988776655',
Email: '[email protected]',
Address: 'a-101 xyz street...'
},
{
_id: new ObjectId("640038ce471e8d22bffd5215"),
Enrollment_No: 6001,
Name: 'Umang',
Semester: 6,
Branch: 'IT',
Mobile_No: '9988776655',
Email: '[email protected]',
Address: 'a-101 xyz street...'
},
{
_id: new ObjectId("640038ce471e8d22bffd5217"),
Enrollment_No: 6003,
Name: 'Ajay',
Semester: 6,
Branch: 'IT',
Mobile_No: '9944332211',
Email: '[email protected]',
Address: 'c-303 mno colony'
}
]
Sorted documents => [
{
_id: new ObjectId("640038ce471e8d22bffd5214"),
Enrollment_No: 6001,
Name: 'Umang',
Semester: 6,
Branch: 'IT',
Mobile_No: '9988776655',
Email: '[email protected]',
Address: 'a-101 xyz street...'
},
{
_id: new ObjectId("640038ce471e8d22bffd5215"),
Enrollment_No: 6001,
Name: 'Umang',
Semester: 6,
Branch: 'IT',
Mobile_No: '9988776655',
Email: '[email protected]',
Address: 'a-101 xyz street...'
},
{
_id: new ObjectId("640038ce471e8d22bffd5216"),
Enrollment_No: 6002,
6th Semester IT (SSEC, Bhavnagar) Page : 6
3161611- Advanced Web Programming | 2022-23 | Enrolment No: _______________
Name: 'Dhaval',
Semester: 6,
Branch: 'IT',
Mobile_No: '9876543210',
Email: '[email protected]',
Address: 'b-202 pqr colony'
},
{
_id: new ObjectId("640038ce471e8d22bffd5217"),
Enrollment_No: 6003,
Name: 'Ajay',
Semester: 6,
Branch: 'IT',
Mobile_No: '9944332211',
Email: '[email protected]',
Address: 'c-303 mno colony'
},
{
_id: new ObjectId("640038ce471e8d22bffd5218"),
Enrollment_No: 6004,
Name: 'Bansi',
Semester: 6,
Branch: 'IT',
Mobile_No: '9876543210',
Email: '[email protected]',
Address: 'x-401 abc colonywxyz colony'
},
{
_id: new ObjectId("640038ce471e8d22bffd5219"),
Enrollment_No: 6005,
Name: 'Binal',
Semester: 6,
Branch: 'IT',
Mobile_No: '9876543210',
Email: '[email protected]',
Address: 'x-101 abc colony'
}
]

6th Semester IT (SSEC, Bhavnagar) Page : 7


3161611- Advanced Web Programming | 2022-23 | Enrolment No: _______________

update.js

const { MongoClient } = require('mongodb');


const url = 'mongodb://127.0.0.1:27017';
const client = new MongoClient(url);
const dbName ='practiceDB';
async function main() {
await client.connect();
console.log('Connected successfully to server');
const db = client.db(dbName);
const collection = db.collection('students');

// updateOne() Operation...
const updateResult = await collection.updateOne({ Name: "Dhaval" }, { $set: { Enrollment_No: 6007 } });
console.log('Updated documents =>', updateResult);

// updateMany() Operation...
const updateResult1 = await collection.updateMany({ Enrollment_No: 6001 }, { $set: { name: "Keval" } });
console.log('Updated documents =>', updateResult1);
}
main()
.then(console.log)
.catch(console.error)
.finally(() => client.close());

Output:

PS D:\2023 EVEN\AWP\Lab Session\Practical 15> node update.js


Connected successfully to server
Updated documents => {
acknowledged: true,
modifiedCount: 1,
upsertedId: null,
upsertedCount: 0,
matchedCount: 1
}
Updated documents => {
acknowledged: true,
modifiedCount: 2,
upsertedId: null,
upsertedCount: 0,
matchedCount: 2
}

6th Semester IT (SSEC, Bhavnagar) Page : 8


3161611- Advanced Web Programming | 2022-23 | Enrolment No: _______________

delete.js

const { MongoClient } = require('mongodb');


const url = 'mongodb://127.0.0.1:27017';
const client = new MongoClient(url);
const dbName ='practiceDB';
async function main() {
await client.connect();
console.log('Connected successfully to server');
const db = client.db(dbName);
const collection = db.collection('students');

// deleteOne() Operation...
const deleteResult = await collection.deleteOne({Enrollment_No: 6007});
console.log('Deleted documents =>', deleteResult);

// deleteMany() Operation...
const deleteResult1 = await collection.deleteMany({Enrollment_No: 6001});
console.log('Deleted documents =>', deleteResult1);
}
main()
.then(console.log)
.catch(console.error)
.finally(() => client.close());

Output:

PS D:\2023 EVEN\AWP\Lab Session\Practical 15> node delete.js


Connected successfully to server
Deleted documents => { acknowledged: true, deletedCount: 1 }
Deleted documents => { acknowledged: true, deletedCount: 2 }

6th Semester IT (SSEC, Bhavnagar) Page : 9


3161611- Advanced Web Programming | 2022-23 | Enrolment No: _______________
Assessment Rubric:

Parameter of
Poor - 1 Fair - 2 Good - 4 Excellent - 5
Assessment

Included required UI Fair design with


Not included the required
UI Content components but required UI Exceptional catchy UI design.
UI components.
design is messy. components.

The required events The complete code is


Code is clumsy. Poor
Coding Code is partially written. related code is provided with exception
readability.
provided. handling.

Implemented less than Implemented 61-80% Implemented 81-90% All the required functionality
Functionality 60% of the app of the app of the app is implemented with proper
functionality. functionality. functionality. logic and comments.

Practical
Includes all the required files
Documentation Poorly written and not Fairly written but not Fairly written and
content with description and
and submitted on time. submitted on time. submitted on time.
output. Submitted on time.
Submission

Faculty Comments:

Total Points - ________ Faculty Signature - ______________

6th Semester IT (SSEC, Bhavnagar) Page : 10

You might also like