ADBMS Assignment 1
Create employee collection in company database and insert at least 10 employee
information in employee collection containing
eid,ename,disignation,hiredate,salary,hobbies and department. Execute following
queries on employee collection.
test> use company
switched to db company
company> db.employee.insertMany([
... { eid: 1, ename: "Alice", designation: "Analyst", hiredate: new Date("2015-
07-01"), salary: 15000, hobbies: ["reading", "painting"], department: "Sales" },
... { eid: 2, ename: "Bob", designation: "Salesman", hiredate: new Date("2016-
03-12"), salary: 12000, hobbies: ["travelling", "reading"], department: "HR" },
... { eid: 3, ename: "Charlie", designation: "Manager", hiredate: new
Date("2015-06-01"), salary: 20000, hobbies: ["painting", "reading"], department:
"Production" },
... { eid: 4, ename: "David", designation: "Clerk", hiredate: new Date("2015-08-
15"), salary: 9000, hobbies: ["reading", "music"], department: "Sales" },
... { eid: 5, ename: "Eve", designation: "Analyst", hiredate: new Date("2015-09-
23"), salary: 8000, hobbies: ["sports", "reading"], department: "HR" },
... { eid: 6, ename: "Frank", designation: "Salesman", hiredate: new Date("2016-
10-10"), salary: 11000, hobbies: ["travelling", "sports"], department: "Sales" },
... { eid: 7, ename: "Grace", designation: "Clerk", hiredate: new Date("2015-12-
01"), salary: 9500, hobbies: ["reading", "painting"], department: "Sales" },
... { eid: 8, ename: "Henry", designation: "Manager", hiredate: new Date("2016-
01-05"), salary: 25000, hobbies: ["travelling", "sports"], department:
"Production" },
... { eid: 9, ename: "Ivy", designation: "Salesman", hiredate: new Date("2015-
10-01"), salary: 13000, hobbies: ["reading", "music"], department: "Sales" },
... { eid: 10, ename: "Jack", designation: "Analyst", hiredate: new Date("2015-
11-30"), salary: 14000, hobbies: ["reading", "sports","music"] department: "HR" }
... ])
{
acknowledged: true,
insertedIds: {
'0': ObjectId('670220a60156883323c4e4a5'),
'1': ObjectId('670220a60156883323c4e4a6'),
'2': ObjectId('670220a60156883323c4e4a7'),
'3': ObjectId('670220a60156883323c4e4a8'),
'4': ObjectId('670220a60156883323c4e4a9'),
'5': ObjectId('670220a60156883323c4e4aa'),
'6': ObjectId('670220a60156883323c4e4ab'),
'7': ObjectId('670220a60156883323c4e4ac'),
'8': ObjectId('670220a60156883323c4e4ad'),
'9': ObjectId('670220a60156883323c4e4ae')
}
}
a) List the names of analysts and salesmen.
company> db.employee.find({ designation: { $in: ["Analyst", "Salesman"] }
}, { ename: 1, _id: 0 })
[
{ ename: 'Alice' },
{ ename: 'Bob' },
{ ename: 'Eve' },
{ ename: 'Frank' },
{ ename: 'Ivy' },
{ ename: 'Jack' }
b) List the eid,ename and salary from employee collection.
company> db.employee.find({ designation: { $ne: "Manager" } }, { ename:
1, _id: 0 })
[
{ eid: 1, ename: 'Alice', salary: 15000 },
{ eid: 2, ename: 'Bob', salary: 12000 },
{ eid: 3, ename: 'Charlie', salary: 20000 },
{ eid: 4, ename: 'David', salary: 9000 },
{ eid: 5, ename: 'Eve', salary: 8000 },
{ eid: 6, ename: 'Frank', salary: 11000 },
{ eid: 7, ename: 'Grace', salary: 9500 },
{ eid: 8, ename: 'Henry', salary: 25000 },
{ eid: 9, ename: 'Ivy', salary: 13000 },
{ eid: 10, ename: 'Jack', salary: 14000 }
]
c) List names of employees who are not managers.
company> db.employee.find({ designation: { $ne: "Manager" } }, { ename:
1, _id: 0 });
[
{ ename: 'Alice' },
{ ename: 'Bob' },
{ ename: 'David' },
{ ename: 'Eve' },
{ ename: 'Frank' },
{ ename: 'Grace' },
{ ename: 'Ivy' },
{ ename: 'Jack' }
]
d) List the names of employees whose employee numbers are 1,3,7,9.
company> db.employee.find({ eid: { $in: [1, 3, 7, 9] } }, { ename: 1, _id:
0 });
[
{ ename: 'Alice' },
{ ename: 'Charlie' },
{ ename: 'Grace' },
{ ename: 'Ivy' }
]
e) List the names of all employees those having reading as a second hobby.
company> db.employee.find({ "hobbies.1": "reading" }, { ename: 1, _id:
0 });
[ { ename: 'Bob' }, { ename: 'Charlie' }, { ename: 'Eve' } ]
f) List employee names for those who have joined between 30 June and 31 Dec
2015.
company> db.employee.find({ hiredate: { $gte: new Date("2015-06-30"),
$lte: new Date("2015-12-31") } }, { ename: 1, _id: 0 });
[
{ ename: 'Alice' },
{ ename: 'David' },
{ ename: 'Eve' },
{ ename: 'Grace' },
{ ename: 'Ivy' },
{ ename: 'Jack' }
]
g) List the different designations in the company.
company> db.employee.distinct("designation");
[ 'Analyst', 'Clerk', 'Manager', 'Salesman' ]
h) List the eid,ename,salary of all employees whose salary is less than 10000.
company> db.employee.find({ salary: { $lt: 10000 } }, { eid: 1, ename: 1,
salary: 1, __id: 0 });
[
{ eid: 4, ename: 'David', salary: 9000 },
{ eid: 5, ename: 'Eve', salary: 8000 },
{ eid: 7, ename: 'Grace', salary: 9500 }
]
i) List the name and designation of the employee who works in production
department.
company> db.employee.find({ department: "Production" }, { ename: 1,
designation: 1, _i_id: 0 });
[
{ ename: 'Charlie', designation: 'Manager' },
{ ename: 'Henry', designation: 'Manager' }
]
j) List the all employees whose name start with "A" letter.
company> db.employee.find({ ename: /^A/ }, { ename: 1, _id: 0 });
[ { ename: 'Alice' } ]
k) List the all employees whose name containing "sh" string.
company> db.employee.find({ ename: /sh/ }, { ename: 1, _id: 0 });
l) List the all employees whose names either start or end with “S”.
company> db.employee.find({ $or: [{ ename: /^S/ }, { ename: /S$/ }] },
{ ename: 1, _id_id: 0 });
m) List the names of employees whose department is not HR.
company> db.employee.find({ department: { $ne: "HR" } }, { ename: 1,
_id: 0 });
[
{ ename: 'Alice' },
{ ename: 'Charlie' },
{ ename: 'David' },
{ ename: 'Frank' },
{ ename: 'Grace' },
{ ename: 'Henry' },
{ ename: 'Ivy' }
]
n) List the number of employees working in sales department.
company> db.employee.count({ department: "Sales" });
5
o) List the number of designations available in the EMP collections.
company> db.employee.distinct("designation").length;
4
p) List the eid,ename,salary of all employees whose salary in between 10000 to
20000.
company> db.employee.find({ salary: { $gte: 10000, $lte: 20000 } }, { eid:
1, ename: 1, salary: 1, _id: 0 });
[
{ eid: 1, ename: 'Alice', salary: 15000 },
{ eid: 2, ename: 'Bob', salary: 12000 },
{ eid: 3, ename: 'Charlie', salary: 20000 },
{ eid: 6, ename: 'Frank', salary: 11000 },
{ eid: 9, ename: 'Ivy', salary: 13000 },
{ eid: 10, ename: 'Jack', salary: 14000 }
]
q) List the eid,ename of all employees whose salary is gretter than or equal to
15000.
company> db.employee.find({ salary: { $gte: 15000 } }, { eid: 1, ename: 1,
_id: 0 });
[
{ eid: 1, ename: 'Alice' },
{ eid: 3, ename: 'Charlie' },
{ eid: 8, ename: 'Henry' }
]
r) List details of employees whose department is Sales and salary is 10000.
company> db.employee.find({ department: "Sales", salary: 10000 });
s) List the names of employees those having reading and painting hobbies.
company> db.employee.find({ hobbies: { $all: ["reading", "painting"] } },
{ ename: 1, _id: 0 });
[ { ename: 'Alice' }, { ename: 'Charlie' }, { ename: 'Grace' } ]
t) List the first hobby of all employees from the employee collection.
company> db.employee.find({}, { ename: 1, hobbies: { $slice: 1 }, _id: 0 })
[
{ "ename": "Alice", "hobbies": ["reading"] },
{ "ename": "Bob", "hobbies": ["travelling"] },
{ "ename": "Charlie", "hobbies": ["painting"] },
{ "ename": "David", "hobbies": ["reading"] },
{ "ename": "Eve", "hobbies": ["sports"] },
{ "ename": "Frank", "hobbies": ["travelling"] },
{ "ename": "Grace", "hobbies": ["reading"] },
{ "ename": "Henry", "hobbies": ["travelling"] },
{ "ename": "Ivy", "hobbies": ["reading"] },
{ "ename": "Jack", "hobbies": ["reading"] }
]
u) List the names of all employees those having three different hobbies.
company>db.employee.find({ hobbies: { $size: 3 } }, { ename: 1, _id: 0 })
[
{ ename: 'Jack' }
]