Thanks for the help @steevej and Stennie, I was with the mongo support chat for this same problem, because they told me to do the same stuff.
This is the mongoDb information:
MongoDB 4.4.11 Enterprise
The driver is the nodejs driver version: 4.1.1
I changed my code and use aggregation and that work fine. But the normal updateOne didn’t. I ran the code from the shell and had the same results with updateOne command. The modified flag says the change was made but when I refresh in Compass the information isn’t change.
In my react app the code is:
import clientPromise from "../../../lib/mongodb";
const client = await clientPromise
const database = client.db('db_name');
await database.collection('payments')
.updateOne({ “contract”: 'C-7806' }, { $inc: { “Balance”: -200 } }, {upsert: true})
.then(result => {
res.json(result);
})
In the mongodb shell:
db.payments.updateOne({ “contract”: 'C-7806' }, { $inc: { “Balance”: -200 } }, {upsert: true})
The query returns:
{ “acknowledged”: true,
“modifiedCount”: 1,
“upsertedId”: null,
“upsertedCount”: 0,
“matchedCount”: 1 }
To make sure I wasn’t adding extra documents, I had only one document in the collection and same result. No new document and the one existing had modifiedcount: 1 but still no change in the information.
Now with aggregation it is working, but I’m seeing that sometimes the $inc works and other times it doesn’t and I don’t understand why.
This is my aggregation code:
{
'$set': {
'Balance': {
'$subtract': [
'$Balance', amount
]
}
}
}
I run in Compass and again sometimes work and sometimes not… and also if I edit the information in Compass, then original value of Balance is set instead of keeping the value set by the aggregation query??
Could be a bug?