Worked-out Example

This page illustrates details step-by-step guide to implement NOSQL for creation of database, tables and eventually query them to obtain data.

Software Installation

  • Install MongoDB Shell for Windows or Ubuntoo from here.

  • Install MongoDB Compass GUI for Windows or Ubuntoo from here. This a GUI for MongoDB Shell software that helps the database user to review and analysis the datasets available in the database.

  • Instead you can directly use online compliers for practice. Try OneCompiler, MyCompiler, Humongous Compiler

// Connect to MongoDB (assuming you're already connected)

// Create the Campus database if it doesn't exist
use("Campus");

// Define main collections
db.createCollection("campuses");
db.createCollection("departments");
db.createCollection("students");
db.createCollection("courses");

// Insert sample data into campuses collection
db.campuses.insertOne({
    name: "University of Tech",
    location: "Cityville",
    founded: 1950,
    website: "www.techuniversity.edu"
});

// Insert sample data into departments collection  
db.departments.insertMany([
    {name: "Computer Science", campusId: ObjectId("...")},
    {name: "Engineering", campusId: ObjectId("...")}
]);

// Insert sample data into students collection
db.students.insertOne({
    name: "John Doe",
    studentId: "S123456",
    department: ObjectId("..."),
    year: 3,
    gpa: 3.5
});

// Insert sample data into courses collection
db.courses.insertMany([
    {name: "Data Structures", campusId: ObjectId("..."), departmentId: ObjectId("...")},
    {name: "Algorithms", campusId: ObjectId("..."), departmentId: ObjectId("...")}
]);

//Insert 10 student information into students collection
db.students.insertMany([
  { name: "Alice Johnson", studentId: "S001", department: ObjectId("..."), year: 2, gpa: 3.8 },
  { name: "Bob Smith", studentId: "S002", department: ObjectId("..."), year: 3, gpa: 3.7 },
  { name: "Charlie Brown", studentId: "S003", department: ObjectId("..."), year: 1, gpa: 3.9 },
  { name: "David Lee", studentId: "S004", department: ObjectId("..."), year: 2, gpa: 3.6 },
  { name: "Eve Davis", studentId: "S005", department: ObjectId("..."), year: 3, gpa: 3.8 },
  { name: "Frank Williams", studentId: "S006", department: ObjectId("..."), year: 1, gpa: 3.7 },
  { name: "Grace Martin", studentId: "S007", department: ObjectId("..."), year: 2, gpa: 3.9 },
  { name: "Henry Thompson", studentId: "S008", department: ObjectId("..."), year: 3, gpa: 3.6 },
  { name: "Ivy Chen", studentId: "S009", department: ObjectId("..."), year: 1, gpa: 3.8 },
  { name: "Jack Harris", studentId: "S010", department: ObjectId("..."), year: 2, gpa: 3.7 }
]);

db.students.find();

Last updated