Home > Software design >  More efficient way to update subdocuments, NodeJS and Mongoose
More efficient way to update subdocuments, NodeJS and Mongoose

Time:12-23

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.

// 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, "billingProfile._id":req.params.billingProfileID}, {$set: {"billingProfile.$.city" : req.body.city}}, {new:true})

What I logically want would be the following however it results in an error...

// Setting req.body equal to ".$" results in an error
const billingProfile = await User.findOneAndUpdate({_id:req.user._id, "billingProfile._id":req.params.billingProfileID}, {$set: {"billingProfile.$" : req.body}}, {new:true})
    
Updating the path 'billingProfile.$.updatedAt' would create a conflict at 'billingProfile.$'

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 :)

FYI - I'm using "mongoose": "^6.1.1"

CodePudding user response:

It's because of mongoose schema { timestamps: true } option, which will automatically set updatedAt field when update happens.

One alternative way is to manage createdAt and updatedAt of billingProfileSchema manually.

Or just set { timestamps: false } for billingProfileSchema

  • Related