0

Say I have a document that looks a bit like this.

{
  _id: ObjectId,
  name: 'corvid',
  games: [{
    name: 'world-of-warcraft',
    lastLogin: ISODate
  }, {
    name: 'starcraft',
    lastLogin: ISODate
  }, {
    name: 'overwatch',
    lastLogin: ISODate
  }],
  roles: {
    'world-of-warcraft': ['player', 'pvp'],
    'starcraft': ['player', 'competitive'],
    'overwatch': ['player']
  }
}

I am trying to find every game in this user hasn't logged into in over 1 week and mark it to inactive. The following is my basic operation.

Users.update({
  $or: Groups.find().map(group => ({
    [`roles.${group.name}`]: 'player',
    'games': {
      $elemMatch: {
        name: group.name,
        lastLogin: {
          $lt: moment().subtract(1, 'week').toDate()
        }
      }
    }
  }))
}, {
  $set: { 'games.$.status': 'inactive' }
}, {
  multi: true
});

However, when it updates, it will only update a single match within the array. I just want it to perform this action for EVERY entry in the array.

Is there a way to do this?

corvid
  • 10,733
  • 11
  • 61
  • 130

0 Answers0