-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtpe_nosql.js
executable file
·136 lines (81 loc) · 4.32 KB
/
tpe_nosql.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
conn = new Mongo();
db = conn.getDB("ic");
// NOM: SAMANI Darix
// port mongodb important
// pour afficher le résultant d'une requête dans le terminal, affectez le résultant
// dans une variable cursor ensuite utilisez forEach(printjson) pour l'affichage
//
// exemple q2 = db.getCollection('etudiants').update({nom: 'DOUWE VINCENT'}, {$set: {nom:'DOUWE H. Vincent'}})
// q2.forEach(printjson)
// Rassurez-vous que mongodb toune sur le port 27017 avant de charger le script javascript
db = connect("localhost:27017/ic");
// Question 2.1
db.getCollection('etudiants').update({nom: 'DOUWE VINCENT'}, {$set: {nom:'DOUWE H. Vincent'}})
// Question 2.2
db.getCollection('etudiants').find({matricule: '00E300FS'}, {_id:0, nom:1,})
// Question 2.3
db.getCollection('etudiants').aggregate([
{$match: {matricule: '00E300FS'}},
{$project: {_id:0, count:{$size: '$notes'}}}
])
// Question 2.4
db.getCollection('etudiants').find({niveau: {$exists: true}}).count()
// Question 2.5
db.getCollection('etudiants').aggregate([
{$group: {_id:'$niveau.intitule', total: {$sum:1}}}
])
// Question 2.6
db.getCollection('etudiants').aggregate([
{$addFields : {moy_arith: {$avg: '$notes.valeur'}}},
{$project: {_id:0, nom: 1, matricule:1, moy_arith: 1}},
])
// Remarque :
// pour simplifier l'ecriture et la lecture des requetes ci-dessous nous avons utilise le client robot3t
// Question 2.7
db.getCollection('etudiants').aggregate([
{$match: {matricule: '00E300FS'}},
{$addFields: {CC: {$arrayElemAt: [{$filter: {input:"$notes", as: "notes", cond: { $and: [{$eq: ["$$notes.cours.code", 'ITEL101']}, {$eq: ["$$notes.evaluation.code", 'CC'] }] }}}, 0]},
TPE: {$arrayElemAt: [{$filter: {input:"$notes", as: "notes", cond: { $and: [{$eq: ["$$notes.cours.code", 'ITEL101']}, {$eq: ["$$notes.evaluation.code", 'TPE'] }] }}}, 0]},
EE: {$arrayElemAt: [{$filter: {input:"$notes", as: "notes", cond: { $and: [{$eq: ["$$notes.cours.code", 'ITEL101']}, {$eq: ["$$notes.evaluation.code", 'EE'] }] }}}, 0]} }},
{$project: {nom:1,EE:1,TPE:1,EE:1, moy: {$add: [{$multiply: [0.3, "$CC.valeur"]},{$multiply:[0.2, "$TPE.valeur"]},{$multiply:[0.5, "$EE.valeur"]}]}}}
], {
allowDiskUse: true
})
// Question 2.8
db.getCollection('etudiants').aggregate([
{$unwind: '$notes'},
{$group: {_id:{matricule:'$matricule', not:'$notes.cours.code'}, notes_par_ue: {$push:'$notes'},avg:{$avg: '$notes.valeur'}, count: {$sum:1} }},
{$project:{ notes_par_ue:1,'_id.matricule':1,
EE:{$arrayElemAt: [{$filter:{input: '$notes_par_ue', as:'note', cond:{$eq:['$$note.evaluation.code','EE']}}},0]},
TPE: {$arrayElemAt: [{$filter:{input: '$notes_par_ue', as:'note', cond:{$eq:['$$note.evaluation.code','TPE']}}},0]},
CC: {$arrayElemAt: [{$filter:{input: '$notes_par_ue', as:'note', cond:{$eq:['$$note.evaluation.code','CC']}}},0]}
}},
{$project: {notes_par_ue:1,EE:1,TPE:1,CC:1,'_id.matricule':1,
moy: {$add: [{$multiply: [0.3, "$CC.valeur"]},{$multiply:[0.2, "$TPE.valeur"]},{$multiply:[0.5, "$EE.valeur"]}]}
}}
], {
allowDiskUse: true
})
// Question 2.9
db.getCollection('etudiants').aggregate([
{$unwind: '$notes'},
{$group: {_id:{matricule:'$matricule', note_par_ue:'$notes.cours.code'}, notes_par_ue: {$push:'$notes'},avg:{$avg: '$notes.valeur'}, count: {$sum:1} }},
{$project:{ notes_par_ue:1,'_id.matricule':1,
EE:{$arrayElemAt: [{$filter:{input: '$notes_par_ue', as:'note', cond:{$eq:['$$note.evaluation.code','EE']}}},0]},
TPE: {$arrayElemAt: [{$filter:{input: '$notes_par_ue', as:'note', cond:{$eq:['$$note.evaluation.code','TPE']}}},0]},
CC: {$arrayElemAt: [{$filter:{input: '$notes_par_ue', as:'note', cond:{$eq:['$$note.evaluation.code','CC']}}},0]}
}},
{$project: {notes_par_ue:1,EE:1,TPE:1,CC:1,'_id.matricule':1,
moy: {$add: [{$multiply: [0.3, "$CC.valeur"]},{$multiply:[0.2, "$TPE.valeur"]},{$multiply:[0.5, "$EE.valeur"]}]}}},
{$project:{ notes_par_ue:1,CC:1,credit_cap:{
$cond:[{$gte:['$moy', 10]}, '$moy', 0]
}}},
{$group: {_id:'$_id.matricule', total:{$sum:'$CC.cours.credits'}}}
], {
allowDiskUse: true
})
// Question 2.10
db.getCollection('etudiants').update({},
{$inc:{"notes.$[elem].valeur": 2}},
{ arrayFilters: [ { "elem.cours.code": { $eq: "ITEL101" }, "elem.evaluation.code": { $eq: "CC" } } ]}
)