0

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.userDetailsis 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.

maria
  • 207
  • 5
  • 22
  • 56
  • Could you explain bit more with some sample collection and the expected output – Ashh Mar 17 '19 at 17:19
  • is it clearer now @AnthonyWinzlet ? – maria Mar 17 '19 at 17:37
  • Try this `Family.updateOne( { "_id": id, "users.userid" : userData._id }, { "$set": "users.$.score" : UserData.score } ) ` – Ashh Mar 17 '19 at 17:41
  • but lets say i have to do that with 100, instead of updateOne, can i make it to be used within updateMany once, instead of updateOne 100 times? – maria Mar 17 '19 at 17:43
  • Yes you should use updateMany instead of using updateOne 100 times – Ashh Mar 17 '19 at 17:44
  • But how would i rewrite this to allow me to do the updateMany at once? – maria Mar 17 '19 at 18:14
  • `Family.updateMany( { "_id": id, "users.userid" : userData._id }, { "$set": "users.$.score" : UserData.score } )` – Ashh Mar 18 '19 at 05:12

0 Answers0