Updating array of objects in mongodb

I’ve posted this onto StackOverflow, to date no luck. Thought someone in the meteor community might be able to help.

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

"careerHistoryPositions": []

My simple-schema looks like:

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

If console.log form data looks like:

 careerHistoryPositions:  [Object, Object],
    0: Object
    company: "Test company"
    title: "Test Title"
    uniqueId: 1498004350350
    1: Object
    company: "Test company 2"
    title: "Test Title 2"
    uniqueId: 149800433221

My update function:

handleFormSubmit(event) {
    event.preventDefault();
    const { careerHistoryPositions } = this.state;
    
    ProfileCandidate.update({ _id: this.state.profileCandidateCollectionId },
      { $set: {
        careerHistoryPositions
      }
      }
    );
  }

You are missing the value.

ProfileCandidate.update({ _id: this.state.profileCandidateCollectionId },
      { $set: {
        careerHistoryPositions : careerHistoryPositions
      }
      }
    );

ES6 prefers the property shorthand short form.

1 Like

right! :confused:
Did you try blackbox in the schema ?