Updating array of objects in mongodb

后端 未结 1 1463
再見小時候
再見小時候 2021-01-28 09:11

I\'m trying to update an array of objects in my simple-schema. Currently, it removes everything in the database and leaves behind:

\"careerHistoryPositions\": []

相关标签:
1条回答
  • 2021-01-28 10:09

    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
        }
      }
    })
    
    0 讨论(0)
提交回复
热议问题