无法更新对象数组

Can't update Array of objects

我正在尝试使用简单模式创建一个对象数组。在此示例中,一个人有一个 careerHistory 对象,其中填充了个人职位。我不知道如何插入和更新对象数组。它只是错误。我能让它工作的唯一方法是明确,例如。 'careerHistory.position.1.company'.

我正在使用:

路径:mongodb

const ProfileCandidateSchema = new SimpleSchema({
  userId: {
    type: String,
    regEx: SimpleSchema.RegEx.Id
  },
  careerHistory: { type: Array, optional: true },
  'careerHistory.position': { type: Object, optional: true },
  'careerHistory.position.$': { type: Object, optional: true },
  'careerHistory.position.$.company': { type: String, optional: true },
  'careerHistory.position.$.title': { type: String, optional: true }
});

路径:updateForm.js

ProfileCandidate.update(id, { $set: {
    'careerHistory.position.company': this.state['position.company'],
    'careerHistory.position.title': this.state['position.title'],
  }
});

如果要将对象推送到数组,请执行

ProfileCandidate.update(id, 
  { $push: { careerHistory: { position: {
    'company': this.state['position.company'],
    'title': this.state['position.title'],
  }}}
});

如果你想更新特定对象

ProfileCandidate.update({ _id: id, 'careerHistory.position.company': this.state['position.company'] }, { $set: {
    'careerHistory.position.$.title': this.state['position.title'],
  }
});

参见集合$