0% found this document useful (0 votes)
55 views5 pages

Teacher and Employee Database Operations

The document outlines a series of MongoDB commands for creating and managing collections related to teachers, employees, and students. It includes commands for inserting data, querying specific fields, updating records, and deleting entries based on certain criteria. Additionally, it demonstrates how to structure collections and perform various operations to retrieve and manipulate data effectively.

Uploaded by

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

Teacher and Employee Database Operations

The document outlines a series of MongoDB commands for creating and managing collections related to teachers, employees, and students. It includes commands for inserting data, querying specific fields, updating records, and deleting entries based on certain criteria. Additionally, it demonstrates how to structure collections and perform various operations to retrieve and manipulate data effectively.

Uploaded by

miteshsharma301
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

use teacher;

show collections;
[Link]("teacher");
[Link]({_id:1});
[Link](
{
_id:1,
name:"Mitesh",
age:18,
gender:"male",
Subject:['hindi','maths','social'],
dob:"20-10-2004",
Salary:50000,
City:"Vadodara"
});
[Link]({_id:2,name:"Devam",age:18,gender:"male",Subject:
['hindi','maths','social'],dob:"21-09-2002",Salary:10000,City:"Surat"});
[Link]({_id:3,name:"Kevin",age:18,gender:"male",Subject:
['hindi','maths','social'],dob:"22-05-2001",Salary:90000,City:"Pakistan"});
[Link]({_id:4,name:"Ismail",age:18,gender:"male",Subject:
['hindi','maths','social'],dob:"23-06-2006",Salary:70000,City:"Israel"});
[Link]({_id:5,name:"Prince",age:18,gender:"male",Subject:
['hindi','maths','social'],dob:"24-03-2008",Salary:60000,City:"Kabristan"});
[Link]({_id:6,name:"Suraj",age:18,gender:"male",Subject:
['hindi','maths','social'],dob:"24-03-2008",Salary:60000,City:"Kabristan"});
[Link]({_id:7,name:"Kokila",age:80,gender:"male",Subject:
['hindi','maths','social'],dob:"21-09-2002",Salary:10000,City:"Surat"});
[Link]({_id:8,name:"Sanata Baii",age:60,gender:"male",Subject:
['hindi','maths','social'],dob:"22-05-2001",Salary:90000,City:"Pakistan"});
[Link]({_id:9,name:"Mahesh Pakodi",age:47,gender:"male",Subject:
['hindi','maths','social'],dob:"23-06-2006",Salary:70000,City:"Israel"});
[Link]({_id:10,name:"Ballu badmash",age:35,gender:"male",Subject:
['hindi','maths','social'],dob:"24-03-2008",Salary:60000,City:"Kabristan"});
[Link]();

[Link](
2.) Display Name, Subject, Gender and Salary
[Link]({},{name:1,subject:1,gender:1,salary:1});

3. Display teacher, which are from the city “Ahmedabad”.


[Link]({City:"Surat"}).pretty();

4. Display the teacher id, name, city and DOB.


[Link]({},{_id:1,name:1,City:1,dob:1});

5. Display the teachers whose gender is female and teach either “Hindi” or
“English” subject.
[Link]({gender:"male",$or:[{Subject:"hindi"},{Subject:"english"}]});

6. Update all those documents where name of teacher is “Anil” with the new
value of subject as “English”.

[Link]({name:"Suraj"},{$set:{Subject:"english"}});
7. Delete data of all those teachers who were born after 1st January 1980.

8. Remove field age.

9. Display the teachers that do not teach “English” subject and their salary is
more than 30000.

[Link]({Subject:{$ne: "math"},Salary:{$gt: 30000}});

10. Find all the teachers having gender “Male” and display salary for them

[Link]({gender:"male"},{Salary:1});

[Link]().pretty();----To display a record

---------------------------------------Create a new MongoDB database called


“EMPLOYEE”. ------------------------------------------
Within “EMPLOYEE” database, create a collection named EMPLOYEE_MASTER”
assume an appropriate schema consisting of fields like Empno, Name, Designation,
DOJ, Department, Salary, Gender, Skills(array)

use Employee;
show collections;
[Link]("Employee_Master");

1. Insert 7 documents into the above collection.

db.Employee_Master.insert(
{
_id:1,
Empno:1,
Name:"Mitesh",
Designation:"Senior Developer",
DOJ: new Date("2011-01-11"),
Department:"IT",
Salary:70000,
Gender:"Male",
Skills:["Java","DSA"]
});

db.EMPLOYEE_Master.insert(
{
_id:2,
Empno: 101,
Name: "John",
Designation: "HR",
DOJ: new Date("2015-05-10"),
Department: "HR",
Salary: 30000,
Gender: "Male",
Skills: ["Management", "Communication"]
});
db.Employee_Master.insert({
_id:3,
Empno: 102,
Name: "Alice",
Designation: "Engineer",
DOJ: new Date("2017-03-22"),
Department: "IT",
Salary: 25000,
Gender: "Female",
Skills: ["Java", "Python"]
});

db.Employee_Master.insert({
_id:4,
Empno: 103,
Name: "Bob",
Designation: "Scientist",
DOJ: new Date("2014-11-18"),
Department: "Research",
Salary: 40000,
Gender: "Male",
Skills: ["Research", "AI"]
});
db.Employee_Master.insert({
_id:5,
Empno: 104,
Name: "Cathy",
Designation: "Accountant",
DOJ: new Date("2018-02-25"),
Department: "Finance",
Salary: 22000,
Gender: "Female",
Skills: ["Accounting", "Excel"]
});
db.Employee_Master.insert({
_id:6,
Empno: 105,
Name: "Akash",
Designation: "Scientist",
DOJ: new Date("2016-07-30"),
Department: "Research",
Salary: 35000,
Gender: "Male",
Skills: ["AI", "Data Science"]
});
db.Employee_Master.insert({
_id:7,
Empno: 106,
Name: "Amit",
Designation: "Accountant",
DOJ: new Date("2013-01-15"),
Department: "Finance",
Salary: 28000,
Gender: "Male",
Skills: ["Accounting", "Audit"]});

2. Display Name, Department, Gender, and Salary

db.Employee_Master.find({}, { Name: 1, Department: 1, Gender: 1, Salary: 1});


3. Display the List of Employees from the Department "HR"

db.Employee_Master.find({Department:"Finance"});

4. Display the Employee ID, Name, Department, and DOB (assuming DOB is part of the
document, it should be added)

db.Employee_Master.find({}, { Empno: 1, Name: 1, Department: 1, DOB: 1, _id: 0 });

5. Display Employees Whose Gender is Female and Designation is Either "Engineer" or


"Scientist"

db.Employee_Master.find({Gender: "Female",Designation: { $in: ["Engineer",


"Scientist"]}});

6. Update All Documents Where Name of Employee is "Akash" with New Value of
Designation as "Engineer"

db.Employee_Master.updateMany({ Name: "Akash" },{$set: { Designation:


"Engineer" }});

7. Delete All Documents Where DOJ is Before 1st January 1999

db.Emloyee_Master.deleteMany({DOJ: { $lt: new Date("1999-01-01") }});

8. Display Employees Whose Salary is Greater Than 25000 and Have Skills "Java" or
"PHP"

db.Employee_Master.find({Salary: { $gt: 25000 },Skills: { $in: ["Java", "PHP"] }});

9. Find All Employees Having Designation "Engineer" and Display Salary for Them

db.Employee_Master.find({ Designation: "Engineer" }, { Salary: 1, _id: 0 });

10. Display Only Those Documents Where the Name of Employee is "Amit" and
Designation is "Accountant"

db.Employee_Master.find({Name: "Amit",Designation: "Accountant"});

---------------------------------------------------------------------------Day-
2---------------------------------------------------------------------
---------------------------------------------------------------------------Day-
2---------------------------------------------------------------------
---------------------------------------------------------------------------Day-
2---------------------------------------------------------------------
---------------------------------------------------------------------------Day-
2---------------------------------------------------------------------
---------------------------------------------------------------------------Day-
2---------------------------------------------------------------------

use studentmaster;
[Link]("student");
[Link]({_id:1,rollno:1,name:"Akhay",grade:"A",hobbies:['playing
games','social work','mimicry'],doj:"21-09-2002"});
[Link]({_id:2,rollno:2,name:"shruti",grade:"A",hobbies:['riding
bikes','reading book','dance'],doj:"11-09-2001"});
[Link]({_id:3,rollno:3,name:"ujwal",grade:"A",hobbies:['playing
games','social work','mimicry'],doj:"01-05-2002"});
[Link]({_id:4,rollno:4,name:"kirtesh",grade:"B",hobbies:['riding
cars','social work','mimicry'],doj:"21-06-2002"});
[Link]({_id:5,rollno:5,name:"kevin",grade:"C",hobbies:['playing
games','reading book','dance'],doj:"21-07-2002"});
[Link]({_id:6,rollno:6,name:"prince",grade:"B",hobbies:['riding
bikes','social work','mimicry'],doj:"21-08-2002"});
[Link]({_id:7,rollno:7,name:"ravi",grade:"B",hobbies:['playing
games','social work','dance'],doj:"21-09-2002"});
[Link]({_id:8,rollno:8,name:"jisna",grade:"C",hobbies:['playing
games','social work','mimicry'],doj:"21-10-2002"});
[Link]({_id:9,rollno:9,name:"pranjal",grade:"D",hobbies:['watching
movies','social work','mimicry'],doj:"21-11-2002"});
[Link]({_id:10,rollno:10,name:"neha",grade:"C",hobbies:['playing
games','social work','mimicry'],doj:"21-12-2002"});
[Link]();

b) Find the document where in the “StudName” has value “Akhay”.

[Link]({name:"Akhay"}).pretty();

c) Find all documents in proper (like tabular) format. (Without _Id field)

[Link]({},{_id:0});

d) Retrieve only Student Name and Grade.

[Link]({},{_id:0,name:1,grade:1});

e) Retrieve Student Name and Grade of student who is having _id column is 1.

[Link]({_id:1},{_id:1,name:1,grade:1});

f) Add new field “Address” in Student collection.

[Link]({},{$set:{"Address":""}});

g) Find those documents where the Grade is set to ‘VII’.

[Link]({grade:"A"});

h) Find those documents where the Grade is not set to ‘VII’.

[Link]({grade:{$ne:"A"}});

i) Find those documents where the Hobbies is set to either ‘Chess’ or is set to
‘Dancing”.

[Link]({$or:[{hobbies:"Chess"},{hobbies:"Dancing"}]});

j) Find those documents where the Hobbies is set neither to ‘Chess’ nor is set to
‘Dancing”.

[Link]({$nor:[{hobbies:"Chess"},{hobbies:"Dancing"}]});

Common questions

Powered by AI

The query `db.teacher.find({City:"Surat"}).pretty()` efficiently retrieves documents based on city filtering by leveraging MongoDB's find command. However, indexing the 'City' field could further enhance retrieval performance, especially in larger datasets .

To create a collection in MongoDB, the `db.createCollection()` command is used. For inserting multiple documents, the `db.collection.insert()` method is applied, as shown with the `teacher` and `Employee_Master` collections .

To delete employees, use `db.Employee_Master.deleteMany({DOJ: { $lt: new Date("1999-01-01") }})`. This permanently removes records, impacting historical data analysis and requiring caution to avoid data loss .

Implement a version control system by creating a `history` collection tracking changes using `timestamps` and `operations` fields. Ensure each record update archives the previous state with unique identifiers to minimize redundancy and maintain data integrity .

The current method updates subjects by name, such as `db.teacher.update({name:"Suraj"},{$set:{Subject:"english"}})`. This single attribute match could lead to inaccuracies if names are reused or similar. A more robust approach involves combining multiple unique identifiers like `_id` to ensure precise targeting before updating .

Students are filtered using conditions like `db.student.find({$or:[{hobbies:"Chess"},{hobbies:"Dancing"}]})`. While effective, indexing relevant fields could optimize performance by reducing scan time, particularly in large datasets .

The command `db.student.updateMany({},{$set:{"Address":""}})` illustrates adding a new field across documents. Best practices include ensuring universal applicability of new fields or phased integration with data validation to prevent inconsistencies and maintain schema integrity .

The teacher database shows most entries with an age of 18, regardless of salary, indicating a possible data inconsistency. There are outliers such as Kokila (age 80) and Sanata Baii (age 60), suggesting diverse age ranges but unaffected salary differences within younger group categories .

To display specific fields, the command `db.Employee_Master.find({}, { Name: 1, Department: 1, Gender: 1, Salary: 1})` can be used. It retrieves only the specified fields, demonstrating selective field visibility in MongoDB .

Skills are queried with conditions like `db.Employee_Master.find({Salary: { $gt: 25000 },Skills: { $in: ["Java", "PHP"] }})`. This approach can be refined with natural language processing to understand descriptive skill requirements, improving precision in skill-based searches .

You might also like