2 years ago
#76575
Diamonddedo
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
javascript
mongodb
mongoose
0 Answers
Your Answer