Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

99
Views
How to assign multiple unknown properties to a mongoose document

I'm trying to write an update function for my documents from mongoDB, So I'm fetching the document based en filters, want to pass properties from a data object to the document but it looks like Object.assign does not work in this case

My objective is being able to update unknown properties, so that I can have a generic function

async updateData(tableName, filters, data) {
    let table = getModel(tableName)
    console.log("data = ", data)
    console.log("document before assign = ", document)
    Object.assign(document, data)
    console.log("document after assign = ", document) //document is not updated
    // document.save()
}

data and documents looks like this :

data =  {"username": "test"}

document before assign =  {
  _id: new ObjectId("61e05ca91ce111c6db4ed265"),
  username: 'lolilol',
  wallet: '0xD1fB16A13b22835822a491B244404f1'
}

document after assign =  {
  _id: new ObjectId("61e05ca91ce111c6db4ed265"),
  username: 'lolilol',
  wallet: '0xD1fB16A13b22835822a491B244404f1'
}

here is a jsFiddle where I tried to see how Object.assign works : https://jsfiddle.net/tb89nx5p/3/

Is it because i'm assigning an objet to a document ? How can I achieve that otherwise ? Thanks

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!