Im trying to find a good efficient way to update the score of multiple users. So here is what i got:
family.users.map
is mapping trough users
Core.userDetails
is getting the user true property.
if score from userDetails
is not equal to Currentuser
, then push the new score to a datastorage
variable.
After everything is completed ( Promise.all
) , i need to update the properties, but how would i make sure that the score is updated within right userid?
Piece of code:
var DataTostore = [];
var familyPromises = family.users.map(function (CurrentUser) {
return Core.userDetails(CurrentUser.userid).then(function (UserData) {
if (UserData.score != CurrentUser.score) {
/*
Score not equal. need to update the users score.
*/
DataTostore.push({score: UserData.score})
} else {
return true;
}
});
});
return Promise.all(familyPromises).then(function (xx) {
return family.updateMany({_id: family.id},{UPDATE SCORES WITH RIGHT USERIDS})
});
Database schema:
users: [
{
userid: {type: String, default: 'noid'},
joined_date: {type: Number, default: 0},
permission: {type: Number, default: 0},
upgrade: {type: Number, default: 0},
score: {type: Number, default: 0},
},
],
How would i accomplish this?
Update:
i would like to update all the new data on score
, instead of doing individual:
Family.updateOne({_id: id, "users.userid" : userData._id },{$set: "users.score" : UserData.score});
I could be doing the query above, and do something like:
var DataTostore = [];
var familyPromises = family.users.map(function (CurrentUser) {
return Core.userDetails(CurrentUser.userid).then(function (UserData) {
if (UserData.score != CurrentUser.score) {
/*
Score not equal. need to update the users score.
*/
Family.updateOne({_id: id, "users.userid" : userData._id },{$set: "users.score" : UserData.score});
} else {
return true;
}
});
});
return Promise.all(familyPromises).then(function (xx) {
});
But that seems like not a good efficient way of updating multiple documents. I would like a way to store the needed data in DataToStore
, and update all at once.