Practical 15: Write Code To Perform Insert, Find, Update, and Delete Operations On Student Database Using Node - Js and Mongodb
Practical 15: Write Code To Perform Insert, Find, Update, and Delete Operations On Student Database Using Node - Js and Mongodb
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:
Solution:
insert.js
find.js
// Sort() Operation...
const sortResult = await collection.find({}).sort( {Enrollment_No:1}).toArray();
console.log('Sorted documents =>', sortResult);
}
main()
.then(console.log)
Output:
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'
}
]
update.js
// 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:
delete.js
// 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:
Parameter of
Poor - 1 Fair - 2 Good - 4 Excellent - 5
Assessment
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: