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

updateOne(filter, update, { timestamps: { createdAt: false, updatedAt: true } }) #3934

Closed
antony opened this issue Mar 2, 2016 · 2 comments
Labels
new feature This change adds new functionality, like a new method or class
Milestone

Comments

@antony
Copy link

antony commented Mar 2, 2016

Rather than raise two feature requests I'll just add this one with a minor but important extension to the timestamps functionality.

  1. I'd love to be able to say {timestamps: false} in a create, update, etc. statement.

The reasoning for this is that when writing tests, I have to drop to the native driver to override auto-time-stamping, which just makes things messy. I want to create a variety of documents in a variety of created/updated periods.

  1. I'd like to be able to turn 'createdAt' off, and just have 'updatedAt'

Reasoning for this is that we're growing our developers team fast. I wrote the system using ObjectID's created functionality, but I really need updatedAt functionality too. Having two ways to get the created time is going to cause problems, as using createdAt is the obvious one, but using native ObjectID (in my opinion) is the correct one.

@vkarpov15 vkarpov15 added this to the 4.5 milestone Mar 5, 2016
@vkarpov15 vkarpov15 added the new feature This change adds new functionality, like a new method or class label Mar 5, 2016
@seriousManual
Copy link
Contributor

The reasoning for this is that when writing tests, I have to drop to the native driver to override auto-time-stamping, which just makes things messy. I want to create a variety of documents in a variety of created/updated periods.
When I understand you correctly you have the issue that it is not possible to specify which date is set via the autotimestamping mechanism?

I like to use sinon fake timers for exactly this:

    var clock = sinon.useFakeTimers() //resets the clock to 1979/01/01

    var testSchema = new mongoose.Schema({ foo: String });
    var TestModel = mongoose.model('test', testSchema);

    var firstTestInstance = new TestModel({foo: 'bar'})
    clock.tick(3600000) //moves the clock forward by 1h
    var secondTestInstance = new TestModel({foo: 'bax'})

    async.parallel([
        cb => firstTestInstance.save(cb),
        cb => secondTestInstance.save(cb)
    ], _ => {
        TestModel.find({}, (error, result) => console.log(result))
    })

Result would be:

[ { __v: 0, foo: 'bar', _id: 0000000035960fa409c1f17c },
  { __v: 0, foo: 'bax', _id: 00000e1035960fa409c1f17d } ]

Notice how the ObjectID differ albeit having been constructed at nearly the same point in time?

@vkarpov15 vkarpov15 modified the milestones: 4.7, 4.5 Jun 9, 2016
@vkarpov15 vkarpov15 modified the milestones: 4.12, 4.10 May 15, 2017
@vkarpov15 vkarpov15 modified the milestones: 5.x Unprioritized, 5.7 Aug 31, 2019
@vkarpov15
Copy link
Collaborator

We already have support for most of these suggestions as of Mongoose 5.6. For example, you can skip timestamps on an individual update using Model.updateOne(filter, update, { timestamps: false }). See updateOne() docs. We also support turning off updatedAt at the schema level as shown in the schema timestamps docs. However, we don't quite support shutting off just updatedAt or just createdAt on an individual update, which we should.

@vkarpov15 vkarpov15 changed the title Make timestamps more flexible updateOne(filter, update, { timestamps: { createdAt: false, updatedAt: true } }) Aug 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature This change adds new functionality, like a new method or class
Projects
None yet
Development

No branches or pull requests

4 participants