there exist a User Model which has the following fields:
{
......
wallet: {
type: Number,
default: 0,
},
wallet2: {
type: Number,
default: 0,
},
.....
}
I wanted to run a findByIdAndUpdate query on this model that would increase the value of wallet to a given amount - wallet2. I've tried to find a solution using chatGPT & every time it came up with a query that either had a syntax error or it did some totally different thing. here is one of the query it generated:
db.users.updateOne({_id: "12345"}, {$inc: {wallet: { $expr: { $subtract: [ 100, "$wallet2" ] } }}})
which results in an error saying:
MongoServerError: Cannot increment with non-numeric argument: {wallet: { $subtract: [ 100, "$wallet2" ] }}
I also tried using $set but it results in the wallet field being set to the query:
{ $set: { wallet: { $subtract: [ 100, "$wallet2" ] } } }
which results in the document being modified to:
..., wallet: { '$subtract': [ 100, '$wallet2' ] }, ...