Skip to content

Change is not detected when value change from equals string=>num ("1" => 1) or vise versa #9165

@tomyam1

Description

@tomyam1

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

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions