Professional Documents
Culture Documents
Lab 4 Correction
Lab 4 Correction
UNIVERSITY OF TUNIS
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}})
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}})) ;