Skip to content
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

Timestamps are lost when replacing a subdocument w/out any changes #11636

Closed
PenguinToast opened this issue Apr 6, 2022 · 3 comments
Closed
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.

Comments

@PenguinToast
Copy link
Contributor

What is the current behavior?

When replacing an existing subdocument with timestamps enabled, if none of the fields within the subdocument change, then timestamps are lost/not set.

If the current behavior is a bug, please provide the steps to reproduce.

const mongoose = require('mongoose');

const assert = require('assert');

const childSchema = new mongoose.Schema({
    name: String
}, { timestamps: true, _id: false});

const testSchema = new mongoose.Schema({ child: childSchema, age: Number});

async function run() {
    await mongoose.connect('mongodb://localhost:27017');
    await mongoose.connection.dropDatabase();

    const Test = mongoose.model('Test', testSchema);

    const doc = await Test.create({ age: 10, child: { name: 'foo' } });
    assert(typeof doc.child.updatedAt !== "undefined"); // this works fine

    doc.child = { name: "foo" };
    await doc.save();

    assert(typeof doc.child.updatedAt !== "undefined"); // fails
}

run();

What is the expected behavior?

If timestamps are enabled on a subdocument, they should never be undefined after save'ing the document.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Tested this on 6.2.10. This worked as expected in 5.9.11, which we are upgrading from.

@PenguinToast
Copy link
Contributor Author

Potentially related to #11603?

@PenguinToast
Copy link
Contributor Author

Also, running doc.child.markModified("name"); before the save makes this test case pass, but doc.markModified("child.name"); does not.

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Apr 7, 2022
@vkarpov15 vkarpov15 added this to the 6.2.14 milestone Apr 14, 2022
@vkarpov15
Copy link
Collaborator

This was fixed in v6.3.1 with #11603. Please upgrade to >= 6.3.1 for the fix.

@vkarpov15 vkarpov15 removed this from the 6.3.3 milestone May 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

3 participants