title: More efficient way to update subdocuments, NodeJS and Mongoose
body: <p>Thanks for reading.  I'm new and learning mongoose and trying to wrap my head around subdocument updates.  I can update sub-documents but the method I'm using doesn't seem like it would scale all that well and would be cumbersome to setup, but it does work.  I do not want to have to update field by field, ie city, state, zip, country, etc.  I would like to simply replace the fields that have been updated automatically.</p>
  <pre><code>// Sample Object
  {
    addressType: 'home',
    street: '123 Easy Street',
    city: 'Somewhere',
    state: 'NA',
    zip: '90210',
    country: 'USA'
  }
  
  // Schema
  const userSchema = mongoose.Schema({billingProfile: [billingProfileSchema], orders: [orderSchema]}, {timestamps: true, strict: false})
  
  
  // Updating item by item works, BUT doesn't scale
  const billingProfile = await User.findOneAndUpdate({_id:req.user._id, &quot;billingProfile._id&quot;:req.params.billingProfileID}, {$set: {&quot;billingProfile.$.city&quot; : req.body.city}}, {new:true})
  </code></pre>
  <p>What I logically want would be the following however it results in an error...</p>
  <pre><code>// Setting req.body equal to &quot;.$&quot; results in an error
  const billingProfile = await User.findOneAndUpdate({_id:req.user._id, &quot;billingProfile._id&quot;:req.params.billingProfileID}, {$set: {&quot;billingProfile.$&quot; : req.body}}, {new:true})
      
  Updating the path 'billingProfile.$.updatedAt' would create a conflict at 'billingProfile.$'
  </code></pre>
  <p>The method that I would think should work, clearly does not.  I'm hoping someone can help me with a more efficient way of updating subdocuments.  Thank you :)</p>
  <p>FYI - I'm using  <code>&quot;mongoose&quot;: &quot;^6.1.1&quot;</code></p>