mongodb script to set ratings safely

pull/6791/head
Thibault Duplessis 2020-06-06 11:50:16 -06:00
parent 3d0d58f11e
commit 06d5c44429
2 changed files with 41 additions and 19 deletions

View File

@ -1,19 +0,0 @@
var user = 'chessbrahs';
var perf = 'bullet';
var rating = 2527;
var set = {};
set ['perfs.' + perf + '.gl.r'] = rating;
var push = {};
push['perfs.' + perf + '.re'] = {
$each: [NumberInt(rating)],
$position: 0
};
db.user4.update({
_id: user
}, {
$set: set,
$push: push
});

View File

@ -0,0 +1,41 @@
const perf = 'rapid';
const ratings = {
'STL_Caruana': 2773,
'STL_Dominguez': 2786,
'STL_So': 2741,
'STL_Carlsen': 2881,
'STL_Vachier-Lagrave': 2860,
'STL_Grischuk': 2784,
'STL_Aronian': 2778,
'STL_Xiong': 2730
};
for (k of Object.keys(ratings)) {
const rating = ratings[k];
const id = k.toLowerCase();
const user = db.user4.findOne({_id:id});
if (user.perfs[perf] && user.perfs[perf].nb) {
const set = { [`perfs.${perf}.gl.r`]: rating };
const push = {
[`perfs.${perf}.re`]: {
$each: [NumberInt(rating)],
$position: 0
}
};
db.user4.update({_id:id},{$set:set, $push: push});
}
else {
db.user4.update({_id:id},{$set:{
[`perfs.${perf}`]: {
"gl": {
"r": rating,
"d": 150,
"v": 0.06
},
"nb": NumberInt(0),
"re": [ ]
}
}});
}
}