-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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 update method cannot change createdAt field #8619
Comments
Set the 'use strict';
const mongoose = require('mongoose');
mongoose.set('debug', true);
mongoose.set('useFindAndModify', false);
const { Schema } = mongoose;
run().catch(err => console.log(err));
async function run() {
await mongoose.connect('mongodb://localhost:27017/test', {
useNewUrlParser: true,
useUnifiedTopology: true
});
await mongoose.connection.dropDatabase();
const schema = Schema({ name: String }, { timestamps: true });
const Model = mongoose.model('Test', schema);
// Sets `createdAt` to June 1, 2015
await Model.findOneAndUpdate({}, { createdAt: new Date('2015/06/01') }, { timestamps: false });
} |
@vkarpov15 this is not working for me. After making the update with the example above, and querying the fresh document, my |
@sean-hill this was for Mongoose 5. In more recent versions of Mongoose, we've made // Sets `createdAt` to June 1, 2015
await Model.findOneAndUpdate({}, { createdAt: new Date('2015/06/01') }, { timestamps: false, strict: false }); However, we should make |
fix(query): add `overwriteImmutable` option to allow updating immutable properties without disabling strict mode
Its working. @vkarpov15 |
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
Model update method does not change
createdAt
field!If the current behavior is a bug, please provide the steps to reproduce.
I want to change createdAt field to newCreatedAt value. I wrote the bellow script and it wasn't useful:
Instead of
findOneAndUpdate
, I tried withupdate
method too and result is the same.However, this script works!
What is the expected behavior?
Update script should be able to update the
createdAt
field.What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
mongoose: 5.1.3,
node: v10.9.0
Mongo: 4.2.2
The text was updated successfully, but these errors were encountered: