0

I have the following document:

{
   "gameName":"Shooter",
   "details":[
      {
         "submitted":1415215991387,
         "author":"XYZ",
         "subPlayer":{
            "members":{
               "squad1":[
                  {
                     "username":"John",
                     "deaths":0
                  }
               ]
            },
            "gameSlug":"0-shooter"
         }
      }
   ],
   "userId":"foL9NpoZFq9AYmXyj",
   "author":"Peter",
   "submitted":1415215991608,
   "lastModified":1415215991608,
   "participants":[
      "CXRR4sGf5AdvSjdgc",
      "foL9NpoZFq9AYmXyj"
   ],
   "slug":"1-shooterConv",
   "_id":"p2QQ4TBwidjeZX6YS"
}

... and the following Meteor method:

Meteor.methods({
    updateDeaths: function(gameSlug, user, squad) {
            Stats.update({details.subPlayer.gameSlug: gameSlug}, ...}); // ???
    }
});

My goal is to update the field deaths. The method has the argument user which is the user object (username = user.username). Furthermore, the argument squad is the squad name as string, e.g. squad1.

How can I do this?

Any help would be greatly appreciated.

0

1 Answer 1

1

You should use something like this:

db.collection.update(
  {'userId':'foL9NpoZFq9AYmXyj', 'details.subPlayer.gameSlug':'0-shooter'}, 
  {'$set': 
     {'details': [{
          'subPlayer': {
               'members': {
                  'squad1':[{'username':'John','death':1}]
               }
          }
       }]
     }
  }
)

UPD:

Also, maybe a better way is to use find().snapshot().forEach(). But I don't know how Meteor supports it. Example:

Stats.find({'userId':'foL9NpoZFq9AYmXyj', 'details.subPlayer.gameSlug':'0-shooter' }).map(function (e) {
    e.details.forEach(function (d) {
        var squad = 'squad1';
        d.subPlayer.members[squad].forEach(function (s) {
            s.deaths = 10000;
        });
    });
    Stats.update({'_id': e._id}, e);
});
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your help! However, I think this function is not available in Meteor. TypeError: Object [object Object] has no method 'findAndModify'.
@user3475602 I update my answer. You can use db.collection.update() instead of findAndModify()
Thank you! That works. However, now the fields details.submitted, details.author and details.subPlayer.gameSlug will be removed after performing this update. Do you know how I can fix this?
Furthermore, how can I set the squad field dynamically with a String variable? For instance var squad = 'squad1';.
@user3475602 See upd section.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.