Somehow I'm unable to update properties of a mongoose object I fetch from a MongoDB. I'm trying to follow this pattern: Mongoose Docs: Document
This is my code:
// note: getInstances just returns model.find()
let instances: InstanceDocument[] = await this.instanceService.getInstances();
instances.forEach(async (instance, index) => {
console.log(instance);
let deviceCount = await this.instanceService.getDeviceCount(instance._id);
let elementCount = await this.instanceService.getElementCount(instance._id)
instance.deviceCount = deviceCount;
instance.elementCount = elementCount;
await instance.save();
console.log(deviceCount, elementCount, instance);
})
The console.log
prints the correct values for deviceCount
and elementCount
, but the instance object remains unmodified. It still has the unupdated values it has in the database.
Note: this is not a duplicate entry of Unable to add properties to js object, as I'm not trying to create a new object and give it properties.