Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

TUNIS BUSINESS SCHOOL

UNIVERSITY OF TUNIS

LAB 4: No SQL Database


1. Create a database called "Company"
Use Company
2. Create a collection Employee
db.createCollection("Employee")
3. Insert four employees (documents) as follow:
 Name: string
 Fname: string
 professional seniority: integer
 commission: float (decimal in mongodb)
 address:
- Number
- street
- city
- Zip Code
db.Employee.insert({Name:"Tounsi",Fname:"Ala",professionalseniority:12,commission:"100.
5",address: {Number:12,street:"wallstreet",city:"Newyork",Zipcode:2050}})

db.Employee.insert({Name:"Tlili",Fname:"Aline", professionalseniority: 10,commission:


"300.8",address: {Number:10,street:"orange",city:"Toulouse",ZipCode:50}})

db.Employee.insert({Name:"Manaii",Fname:"Fatma",professionalseniority: 9,address:
{Number:12,street:"victory",city:"Tunis",ZipCode:1050}})

db.Employee.insert({Name:"Benzarti",Fname:"Sabrine",professionalseniority: 13,address:
{Number:9,street:"roses",city:"Tunis",ZipCode:1650}})

4. Display all employees in Employee collection.


db.Employee.find()
db.Employee.find().pretty()
5. Display the number of documents in Employee collection
db.Employee.find().count()

6. insert two new employees with the fields name, fname or either prime or
professionalseniority. (two ways)
db.Employee. insert({ name:"Alain ",Fname: " Jolie",professionalseniority :10}) ;
db.Employee.insertOne({name: "Wick", Fname:" John",commission: "150.5"}) ;
7. Display all employees that their first name starts with ‘A’.
db.Employee.find({Fname :/^ A .*/})
TUNIS BUSINESS SCHOOL
UNIVERSITY OF TUNIS

8. Display all employees that their first name starts with ‘A’ or finish with ’A’.
db. Employee. find ({ Fname :/^ A .*|.* A$ /})

9. Display the list of Employees that their First name starts with ‘A’ and has a name length equal
3.
db.Employee.find({ Fname : /^ A [a - z ]{2}$ /}) ;
TUNIS BUSINESS SCHOOL
UNIVERSITY OF TUNIS

10. Display the list of employees that their name start or finish with a vowel.
db.Employee.find ({ name: /^[ AEIOUY ].*[ aeiouy ]$ /})

11. Display the list of employees that their name finish with a vowel.
db.Employee.find ({ Name: /.*[ aeiouy ]$ /})
12. Display the name and first name of each employee with professionalseniority > 10
db.Employee.find ({professionalseniority :{ $gt :10}},{ _id :0,Name :1,Fname :1})

13. Display the name and full address of employees with a street attribute in the address object
db.Employee.find ({{'address .street ': {$exists :true }},{name:1,address:1} )
TUNIS BUSINESS SCHOOL
UNIVERSITY OF TUNIS

14. Display the first three people with the highest seniority value.
db.Employee.find({professionalseniority:{ $exists : true }} , { _id :0}) . sort
({professionalseniority : -1}) . limit (3) ;

15. Display employees whose first name begins with ‘A ‘ and the city of residence is either ‘Tunis’
or ‘Toulouse’
db.employee.find ({ $and : [ { Fname : /^ A /} , { $or : [ { ' address.city ': ' Tunis '} , { '
address.city ': ' Toulouse '} ] } ] })
TUNIS BUSINESS SCHOOL
UNIVERSITY OF TUNIS

16. Update the address of Aline Tlili : new address ({ Number: 20, city: ’Marseille’,
ZipCode: 13015 }). Please note, there will no longer be a street attribute in the address.
db.Employee.updateOne({Fname:'Aline',name:'Tlili'},{$set:{'address.Number' :
20,'address .city': 'Marseille','address. ZipCode: 13015},$unset:{'address.street':1}})

17. calculate and display the sum of seniority for employees with the same city
db.Employee.aggregate({$group:{_id :'$address.city ' , SenioritySum : { $sum : ' $
professionalseniority'}}}) ;

18. calculate and display in descendant order the sum of seniority for employees with the same
city
db.Employee.aggregate({$group:{_id :'$address.city ' , SenioritySum : { $sum : ' $
professionalseniority'}}},{$sort:{_id:-1}})) ;

You might also like