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

mongoose timestamps createdAt added to existing documents upon doc.save() #11024

Closed
guy-evdev opened this issue Nov 29, 2021 · 2 comments
Closed
Assignees
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@guy-evdev
Copy link

nodejs version 14.16.0
mongoose version 5.12.5
MongoDB driver (nodejs) 3.6.5

mongoose timestamps configuration:
{timestamps: {createdAt: 'createdAt', updatedAt: 'lut'}}

Steps for reproducing:

  1. Document without the createdAt field (was created before adding timestamps to the schema).
  2. Document retrieved from DB using findOne() with no field projection
  3. Adding/modifying fields
  4. Saving the document using doc.save()
    After saving, the createdAt field is added to the document with the current date which of course is not the expected behavior since the createdAt field should reflect the actual document creation date (which was created much earlier)

It will not happen when:
Using findAndUpdate (much better the findOne and save - I know)
Using findOne with field projection

As a temporary workaround, I added the following to all schemas using timestamps
createdAt: {type: Date, select: false}
When the above is added, the createdAt field is not added to the existing document upon saving but this is definitely intended as a quick-fix and not a permanent solution since the expected behaviour is that the createdAt field will not be added to an existing document if not specifically requested.

@guy-evdev guy-evdev changed the title BUG: mongoose timestamps createdAt added to existing documents upon doc.save() mongoose timestamps createdAt added to existing documents upon doc.save() Nov 29, 2021
@IslandRhythms
Copy link
Collaborator

The only field being added is the updatedAt timestamps when you include the schema on mongoose 6.0.13
This bug is reproducable in your version of mongoose, 5.12.5
@vkarpov15 How do you want to proceed?

const mongoose = require('mongoose');
const {Schema} = mongoose;
/*
const testSchema = new Schema({
    name: String
});
*/

const testSchema = new Schema({
    name: String
}, {timestamps: {createdAt: 'createdAt', updatedAt: 'lut'}});

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

async function run() {
    await mongoose.connect('mongodb://localhost:27017/', {useNewUrlParser: true,
    useUnifiedTopology: true,});
    // await mongoose.connection.dropDatabase();
    // await Test.create({name: 'Test Testerson'});
    const entry = await Test.findOne();
    entry.name = 'Modified Timestamp names';
    await entry.save();
    console.log(await Test.find())
}

run();

@vkarpov15 vkarpov15 added this to the 6.0.17 milestone Dec 3, 2021
@vkarpov15
Copy link
Collaborator

This issue is fixed in 6.0.0. But still affects all 5.x versions. Please upgrade to 6.0.0 - we'll work on a fix for the 5.x branch.

@vkarpov15 vkarpov15 modified the milestones: 6.1.4, 5.13.14 Dec 26, 2021
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed seen labels Dec 26, 2021
vkarpov15 added a commit that referenced this issue Dec 27, 2021
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