Amit_Hadad
(Amit Hadad)
#1
Im trying to update object in schema by other object. somthing like this:
Resources: {
Gold: Number
}
Workers: {
Efficiency:
{
Mine: Number
}
}
signUpTemplate.updateMany({},
{ $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.
Tarun_Gaur
(Tarun Gaur)
#3
Hello @Amit_Hadad ,
Welcome to The MongoDB Community Forums! 
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
Regards,
Tarun
steevej
(Steeve Juneau)
#4
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
system
(system)
Closed
#5
This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.