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

Update with aggregation pipeline ignores custom timestamps #11151

Closed
mete89 opened this issue Dec 28, 2021 · 1 comment
Closed

Update with aggregation pipeline ignores custom timestamps #11151

mete89 opened this issue Dec 28, 2021 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@mete89
Copy link

mete89 commented Dec 28, 2021

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
UpdateWithAggregationPipeline ignores the custom timestamps in schema and creates updatedAt field instead of updating updated_at

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

const mongoose = require('mongoose');
const assert = require('assert');

run().catch(console.error).then(mongoose.disconnect);

async function run() {
    mongoose.Promise = Promise;
    const mongoUri = 'mongodb://localhost:27017/test';
    await mongoose.connect(mongoUri, {poolSize: 5, useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false, useCreateIndex: true});
    console.log(mongoose.version);

    const book_schema = new mongoose.Schema({
        title: String
    },{
        typeKey: '$type', timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'}
    });

    const Book = mongoose.model('Book', book_schema);
    await Book.deleteMany({});
    const book = new Book();
    book.title = 'Mongoose';
    await book.save();

    await Book.updateMany({}, {title: 'mongoose-with-ordinary-update'});

    const updated_books = await Book.find({updatedAt: {$exists: true}}).lean();
    assert.ok(updated_books.length === 0);
    console.log(updated_books);

    await Book.updateMany({}, [{$set: {title: 'mongoose-with-aggregate-set'}}]);
    const latest_books = await Book.find({updatedAt: {$exists: true}}).lean();
    console.log(latest_books);
    assert.ok(latest_books.length === 0);

    console.log('All assertions passed.');
}

What is the expected behavior?
It should update only updated_at field instead of creating updatedAt as in the schema.

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

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Dec 28, 2021
@IslandRhythms
Copy link
Collaborator

I had to comment out one of the assertions for the whole test to run. The reporter is using mongoose 5.13.14 however this still persists on the latest version of mongoose. Furthermore, on version 5 the commented out assertion passes while on version 6 it fails. For some reason, the filter is being ignored and returning the only document in the database. I logged the result to see if there was an updatedAt property and none exists but for some reason it still returns the document. THe second assertion is correct in that it should be failing as an updatedAt property is being added on top of the updated_at property the user added. Interestingly, they are not the same time.

const mongoose = require('mongoose');
const assert = require('assert');

run().catch(console.error).then(mongoose.disconnect);

async function run() {
    mongoose.Promise = Promise;
    const mongoUri = 'mongodb://localhost:27017/test';
    await mongoose.connect(mongoUri);
    await mongoose.connection.dropDatabase();
    console.log(mongoose.version);

    const book_schema = new mongoose.Schema({
        title: String
    },{
        typeKey: '$type', timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'}
    });

    const Book = mongoose.model('Book', book_schema);
    const book = new Book();
    book.title = 'Mongoose';
    await book.save();
    await Book.updateMany({}, {title: 'mongoose-with-ordinary-update'});

    const updated_books = await Book.find({updatedAt: {$exists: true}}).lean();
    console.log(updated_books, updated_books[0].updatedAt);
   // assert.ok(updated_books.length === 0);
    

    await Book.updateMany({}, [{$set: {title: 'mongoose-with-aggregate-set'}}]);
    const latest_books = await Book.find({updatedAt: {$exists: true}}).lean();
    console.log(latest_books);
    assert.ok(latest_books.length === 0);

    console.log('All assertions passed.');
}

@vkarpov15 vkarpov15 modified the milestones: 6.1.7, 6.1.6 Jan 3, 2022
@vkarpov15 vkarpov15 modified the milestones: 6.1.6, 6.1.7 Jan 10, 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