1

Im trying to increase a value of an object by another value of an object using mongoose library.

Im getting a type cast error becuase it just identifies Resources.Gold as a number but does not find Workers.Efficiency.Mine so it counts it as a String.

Schema:

Resources: {
  Gold: Nuber
}
Workers: {
  Efficiency: {
    Mine: Number
  }
}

Update code:

schema.updateMany({}, {
  $inc: {
    "Resources.Gold": "Workers.Efficiency.Mine"
  }
}, function(err, response) {
  if(err) console.log(err);
  else console.log(response); 
});
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Amit Hadad
  • 13
  • 3
  • Does this answer your question? [Update MongoDB field using value of another field](https://stackoverflow.com/questions/3974985/update-mongodb-field-using-value-of-another-field) – AdamExchange Nov 08 '22 at 00:57
  • No because it does not say how to treat to an object value in increment – Amit Hadad Nov 08 '22 at 18:33

0 Answers0