0

I am trying to determine the correct MongoDB syntax when I want to update two separate properties with the same findOneAndUpdate(). This is what I have (and it's only updating the first property:

await Agency.findOneAndUpdate(
  {
    historicId: data.id_number
  },
  {
    $set: {
      "authorizations.supervisor": authorizationsSupervisor,
      $push: {
        "authorizations.policy": firstPolicyObj,
        "authorizations.policy": secondPolicyObj,
        "authorizations.policy": thirdPolicyObj
      }
    }
  },
  {
    returnNewDocument: true
  }
);

What is the correct way to structure this syntax?

The document model looks like this:

{
   _id: <ObjectId>,
   otherProp: <value>,
   authorizations: {
     supervisor: <ObjectId>,
     department: <value>,
     policy: [ 
       {
         category: <value>,
         requirements: [
           {},
           {}
         ]
       }
     ]
   }
}
Muirik
  • 6,049
  • 7
  • 58
  • 116

1 Answers1

1
$set: {
         "authorizations.supervisor": authorizationsSupervisor,
      },
$push: { 
        "authorizations.policy": { $each: [ firstPolicyObj, secondPolicyObj, thirdPolicyObj] 
      }
Jesper1
  • 78
  • 7