Home > Net >  MongoDB: $Push Does not Update Document
MongoDB: $Push Does not Update Document

Time:08-03

I am trying to update a MongoDB document using the $push command. It instead creates an additional array object instead of appending it to an existing one. When I used $set, it overwrites the existing data and replaces it with the data that should have been appended.

Here is my MongoDB document:

{
"_id":{"$oid":"62e89560059e092e53796b97"},
"submissionDate":"$date":"2022-08-01T00:00:00.000Z"},
"userID":"B0k3qww4pdRGWqPaB2xTLF25A5n1",
"TrainWise":{
  "competitionTrack":[{
    "competitionName":"UCLA Match",
    "positiveMood":["Energized","Breathing","Hopeful"],
    "negativeMood":["Breathless","Nervous"],
    "submissionDate":{"$date":"2022-08-01T00:00:00.000Z"},
    "competitionDate":"08/01/2022",
    "competitionOpponent":"USC",
    "opponentRanking":"14",
    "competitionType":"Amateur",
    "eventRound":"3"}]}
}

Here is what I have tried to use to update the document

competition_track_data = {
  'rateWorthiness': {
    'questionPrompt': 'How strong was my feeling of worthiness and belief that I could win?',
    'questionResponse': '6'}, 
  'ratePhysicalPrep': {
    'questionPrompt': 'How well did I physically prepare for this competition?', 
    'questionResponse': '8'}, 
  'rateMentalPrep': {
    'questionPrompt': 'How well did I mentally prepare for this competition?', 
    'questionResponse': '6'}
}

This is my code for updating the MongoDB

trainwise_management_collection.update_one({
  "TrainWise.competitionTrack": { 
    "$elemMatch" : { "competitionName" : TrainWise.competitionTrack.competitionName, }},
  "userID" : user_id, 
},
{       
  "$push": {
    "TrainWise.competitionTrack.$": [ competition_track_data ],
   }
})

When I run this code, I get this error: "The field 'TrainWise.competitionTrack.0' must be an array but is of type object in the document." However, if I removed the $ sign, it does not update the matching document.

What am I doing wrong?

CodePudding user response:

$push is for when you want to append a value to an array. What you're aiming to do is $set fields on a document.

As you've figured out, you need to use the $ positional operator to tell MongoDB which document in the array you want to update.

The following code example is similar to the MongoDB documentation for updating documents in an array with the positional operator:

trainwise_management_collection.update_one({
  "TrainWise.competitionTrack": { 
    "$elemMatch" : { "competitionName" : TrainWise.competitionTrack.competitionName, }},
  "userID" : user_id, 
},
{       
  "$set": {
    "TrainWise.competitionTrack.$.rateWorthiness": {
      'questionPrompt': 'How strong was my feeling of worthiness and belief that I could win?',
      'questionResponse': '6'
    },
    "TrainWise.competitionTrack.$.ratePhysicalPrep": {
      'questionPrompt': 'How well did I physically prepare for this competition?', 
      'questionResponse': '8'
    },
    "TrainWise.competitionTrack.$.rateMentalPrep": {
      'questionPrompt': 'How well did I mentally prepare for this competition?', 
      'questionResponse': '6'
    }
  }
})
  • Related