Department of Computing: Lab 06: Node - Js Mongodb
Department of Computing: Lab 06: Node - Js Mongodb
Department of Computing: Lab 06: Node - Js Mongodb
Node.js can be used in database applications. One of the most popular NoSQL database is
MongoDB.
Objectives
This lab will get you familiar with the node.js MongoDB environment.
Tools/Software Requirement
Node.js, Notepad
Description
To be able to experiment with the code examples, you will need access to a MongoDB database.
To download and install the official MongoDB driver, open the Command Terminal and execute
the following:
var mongo = require('mongodb');
Helping Material
Slides of Lecture 7
https://www.w3schools.com/nodejs/nodejs_mongodb.asp
Task 1: Create a database named "mydb". Save the code in a file called
"demo_create_mongo_db.js" and run the file.
Task 2: Create a collection called "customers". Save the code in a file called
"demo_mongodb_createcollection.js" and run the file.
Task 3: Insert a document in the "customers" collection. Save the code in a file called
"demo_mongodb_insert.js" and run the file.
Task 4: Insert multiple documents in the "customers" collection. Save the code in a file called
"demo_mongodb_insert_multiple.js" and run the file.
Task 5: Insert three records in a "products" table, with specified _id fields. Save the code in a
file called "demo_mongodb_insert_id.js" and run the file.
Task 6: Find the first document in the customer’s collection. Save the code in a file called
"demo_mongodb_findone.js" and run the file.
Task 7: Return the fields "name" and "address" of all documents in the customers collection
Task 8: Find documents with the address "Park Lane 38". Save the code in a file called
"demo_mongodb_query.js" and run the file.
Task 9: Sort the result alphabetically by name. Save the code in a file called "demo_sort.js" and
run the file.
Task 10: Delete the document with the address "Mountain 21". Save the code in a file called
"demo_delete.js" and run the file.
Task 11: Delete all documents were the address starts with the letter "O". Save the code in a file
called "demo_delete_many.js" and run the file
Task 12: Delete the "customers" table. Save the code in a file called "demo_drop.js" and run the
file.
Task 13: Update the document with the address "Valley 345" to name="Mickey" and
address="Canyon 123". Save the code in a file called "demo_update_one.js" and run the file
Task 14: Consider you have a "customers" collection. Limit the result to only return 5
documents. Save the code above in a file called "demo_mongodb_limit.js" and run the file.
Solution
Task Code:
Deliverables
Compile a single word document by filling in the solution part and submit this Word file on
LMS. This lab grading policy is as follows: The lab is graded between 0 to 10 marks. The
submitted solution can get a maximum of 5 marks. At the end of each lab or in the next lab, there
will be a viva related to the tasks. The viva has a weightage of 5 marks. Insert the
solution/answer in this document. You must show the implementation of the tasks in the
designing tool, along with your complete Word document to get your work graded. You must
also submit this Word document on the LMS. In case of any problems with submissions on
LMS, submit your Lab assignments by emailing it to Ms. Sadia: sadia.amir@seecs.edu.pk.