Error when increasing value by other field

Im trying to update object in schema by other object. somthing like this:

Resources: {
Gold: Number
Workers: {
     Mine: Number

    { $inc:

        {"Resources.Gold": "Workers.Efficiency.Mine"}

    }, function(err, response){

        if(err) console.log(err);

        else console.log(response);


Im getting a type cast error since the Workers.Efficienct.Mine is not getting recognized as an object so it count as String.

Hello @Amit_Hadad ,

Welcome to The MongoDB Community Forums! :wave:

Could you please provide below details for me to understand your use-case further?

  • Some sample documents
  • Expected output/Expected updated documents after query is executed successfully
  • Error that you are getting
  • MongoDB version
  • Driver version


I do not know if there is another way but you could use update with aggregation pipeline.

Something like this untested code:

c.updateMany( {} ,
    { "$set" : {
      "Resources.Gold" : { $add : ["$Resources.Gold","$Workers.Efficiency.Mine"]}
1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.