- 
          
- 
        Couldn't load subscription status. 
- Fork 3.9k
Closed
Labels
confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.We've confirmed this is a bug in Mongoose and will fix it.
Milestone
Description
Do you want to request a feature or report a bug? bug
What is the current behaviour?
When setting value on a mixed schema when the only change is number=>string or string=>number with the same value, e.g. "1" => 1 or 1 => "1" the change is not detected.
import * as Mongoose from 'mongoose'
import * as Assert from 'assert'
interface IDoc extends Mongoose.Document {
    obj: {
        key: string | number
    }
}
async function main() {
    const schema = new Mongoose.Schema({
        obj: {},
    })
    const Model = Mongoose.model<IDoc>('tmp', schema);
    const doc = await Model.create({
        obj: {
            key: '1',
        },
    });
    Assert.strictEqual(
        (await Model.findById(doc)).toObject().obj.key,
        '1'
    );
    doc.obj = {
        key: '2'
    };
    await doc.save();
    Assert.strictEqual(
        (await Model.findById(doc)).toObject().obj.key,
        '2'
    );
    doc.obj = {
        key: 2
    };
    // change is not detected, doc.modifiedPaths() is empty
    await doc.save();
    // this fails because '2' !== 2
    Assert.strictEqual(
        (await Model.findById(doc)).toObject().obj.key,
        2
    );
}
main().catch((err) => {
    console.error(err);
    process.exit(1);
});What is the expected behavior?
Should have detected the change.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
mongoose: 5.9.19
mongodb: 3.5.9
node: 12.13.0
Metadata
Metadata
Assignees
Labels
confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.We've confirmed this is a bug in Mongoose and will fix it.