New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mongoose schema setter not being called when using $inc #13158
Comments
const mongoose = require('mongoose');
const testSchema = new mongoose.Schema({
money: {
balance: {
type: Number,
set: (v) => Number(v)?.toFixed(4),
default: 0,
},
},
});
const Test = mongoose.model('Test', testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
await Test.create({
money: { balance: -4}
});
const doc = await Test.findOne();
console.log(doc.money.balance);
const update = await Test.findOneAndUpdate({}, {$inc: { 'money.balance': 1.5432 }}, { runValidators: true, returnDocument: 'after' });
console.log('what is update', update);
}
run(); output:
|
@IslandRhythms the issue with your script is that you're rounding to 4 decimal places, and Round to 2 and your script works fine: const mongoose = require('mongoose');
const testSchema = new mongoose.Schema({
money: {
balance: {
type: Number,
set: (v) => Number(v)?.toFixed(2),
default: 0,
},
},
});
const Test = mongoose.model('Test', testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
await Test.create({
money: { balance: -4}
});
const doc = await Test.findOne();
console.log(doc.money.balance);
const update = await Test.findOneAndUpdate({}, {$inc: { 'money.balance': 1.5432 }}, { runValidators: true, returnDocument: 'after' });
console.log('what is update', update);
}
run(); Output:
Also works fine with negative numbers: const update = await Test.findOneAndUpdate({}, {$inc: { 'money.balance': -1.5432 }}, { runValidators: true, returnDocument: 'after' }); Output:
However, I think the issue is that using the document const mongoose = require('mongoose');
const testSchema = new mongoose.Schema({
money: {
balance: {
type: Number,
set: (v) => Number(v)?.toFixed(2),
default: 0,
},
},
});
const Test = mongoose.model('Test', testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
await Test.create({
money: { balance: -4}
});
const doc = await Test.findOne();
console.log(doc.money.balance);
doc.$inc('money.balance', -1.5432);
await doc.save();
const update = await Test.findOne({ _id: doc._id });
console.log('what is update', update);
}
run(); Output:
|
@vkarpov15, if the document $inc() method doesn't call setters, why does it call it in your first example? Here's what the $inc returns in different cases: -4 + 1.5432 = (1 decimal) -2.5 ✔️ |
@nadirzebiri The reason why you're seeing |
@vkarpov15 ok so how not to have this floating error? $inc looks like calling setters but not in some cases. Does the error come from mongoose or MangoDB? |
fix(document): apply setters on resulting value when calling Document.prototype.$inc()
@nadirzebiri we have a fix in #13178, will ship the fix later today. |
Prerequisites
Mongoose version
^6.9.0
Node.js version
18.12.1
MongoDB version
5.0.15
Operating system
Windows
Operating system version (i.e. 20.04, 11.3, 10)
11
Issue
I run in a problem. I have defined a setter in my model:
When I try increasing balance of a user, it does format the number when the value is positive but sometimes not when its negative, which is weird.
Thanks for helping me
The text was updated successfully, but these errors were encountered: