Updating array of objects in mongodb

故事扮演 提交于 2019-12-02 08:43:00

I managed to fix this by mapping over my object and running 2 separate updates. The first removes the old element and the second adds the updated version. I'm sure there is a better way to do this, however, this does seem to work.

handleFormSubmit(event) {
  event.preventDefault();
  const { careerHistoryPositions } = this.state;

  ProfileCandidate.update({_id: this.state.profileCandidateCollectionId}, { $unset: {
    'careerHistoryPositions': {}
  }
})        


const updatePosition = this.state.careerHistoryPositions.map((position) => {
  ProfileCandidate.update({_id: this.state.profileCandidateCollectionId}, { $push: {
    'careerHistoryPositions': {
      company: position.company,
      title: position.title,
      uniqueId: position.uniqueId
    }
  }
})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!