How to update an array of objects (Noob question)


#1

I am using Collection2 to define a User Schema with the following property:

//Schemas.User
tags: {
  type: [Schemas.Tags],
  optional: true
}
//...

Schemas.Tags = new SimpleSchema({
  name: {
    type: String
  }
  correct: {
    type: Number,
    optional: true
  }
  wrong: {
    type: Number,
    optional: true
  }
});
}

How should I go about updating the User property to add Tag objects if they do not already exist but simply increment either the correct or wrong property if a Tag object with same name already exists in the Users.tags array?

Thank you for your help. I am still getting to grips with the whole Collection2 thing. =)


#2

I’d find out if $addToSet indicates whether it succeeded or not and then use that to $inc the appropriate field.

However, this guy’s having issues updating nested objects much like what you’re doing here so if it doesn’t work the two of you’ll be in the same boat!