🎓 Get Full Quality Study Materials & Lab Manuals — Follow our blog using the button below!  |  📺 Subscribe our YouTube Channel "Lectures by Ravula Govardhan" for Programming Contents!  | 
● NEW Experiment 1 to 13 Full Stack Development Lab Programs available Read it here →
“à°ˆ à°°ోà°œు à°¨ేà°°్à°šుà°•ుà°¨్à°¨ à°šిà°¨్à°¨ à°µిà°·à°¯ం, à°°ేపటి à°ªెà°¦్à°¦ à°µిజయాà°¨ిà°•ి à°ªుà°¨ాà°¦ి.”

Experiment: Using MongoDB Compass to Perform Database Operations

Experiment · MongoDB Lab

CRUD Operations in MongoDB using Node.js and Compass

Aim

To demonstrate the core CRUD operations — Create, Insert, Read, Update, and Delete — in MongoDB using the Compass GUI.

Procedure

  1. Open MongoDB Compass and connect to the local server at mongodb://localhost:27017.
  2. Create a new database called college.
  3. Inside it, create a collection named students.
  4. Insert a few student documents into the collection.
  5. Run Read, Update, and Delete operations on those documents.
  6. Check MongoDB Compass to confirm each operation went through correctly.

Algorithm

  1. Start MongoDB Compass and connect to the MongoDB server.
  2. Create the college database.
  3. Create the students collection within it.
  4. Insert student records into the collection.
  5. Display all student records.
  6. Update a student record.
  7. Delete a student record.
  8. Display the final set of records.
  9. Stop.
01 Install required software

You'll need Node.js, MongoDB Community Server, MongoDB Compass (optional, for viewing data), and VS Code.

Check Node.js in the VS Code terminal:

Terminal
node -v
npm -v

Check MongoDB by running:

Terminal
mongosh

If mongosh opens successfully, MongoDB is running and available.

02 Create a project

Open VS Code and create a folder named mongodb-lab. Open this folder in VS Code, then open a new terminal (Terminal → New Terminal) and run:

Terminal
npm init -y

Then install the MongoDB driver:

Terminal
npm install mongodb
03 Create app.js

Create a file named app.js and use this code:

app.js
const { MongoClient } = require("mongodb");

const client = new MongoClient("mongodb://localhost:27017");

async function main() {
    await client.connect();

    const db = client.db("college");
    const students = db.collection("students");

    // Insert
    await students.insertMany([
        { name: "Ravi", age: 20, branch: "CSE" },
        { name: "Sita", age: 21, branch: "CSE" }
    ]);

    // Read
    console.log("Students:");
    console.log(await students.find().toArray());

    // Update
    await students.updateOne(
        { name: "Ravi" },
        { $set: { age: 21 } }
    );

    // Delete
    await students.deleteOne({ name: "Sita" });

    // Final records
    console.log("Final Records:");
    console.log(await students.find().toArray());

    await client.close();
}

main();
04 Run the program

Make sure the MongoDB server is running, then in the VS Code terminal:

Terminal
node app.js

Expected Output

Students:
[
  { _id: ..., name: 'Ravi', age: 20, branch: 'CSE' },
  { _id: ..., name: 'Sita', age: 21, branch: 'CSE' }
]

Final Records:
[
  { _id: ..., name: 'Ravi', age: 21, branch: 'CSE' }
]

The _id field is automatically generated by MongoDB.

05 View the database in MongoDB Compass

Open MongoDB Compass and connect to mongodb://localhost:27017. You'll see the following structure:

college
  └── students
      └── Ravi

The final document looks like:

{ name: "Ravi", age: 21, branch: "CSE" }

Result: The basic CRUD operations — Create, Read, Update, and Delete — were successfully demonstrated using MongoDB Compass.

MongoDB Node.js CRUD MongoDB Compass Database Lab

1 comment: