-
-
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
When using replaceOne, the createdAt timestamp is being updated like the updatedAt #14309
Closed
2 tasks done
Labels
docs
This issue is due to a mistake or omission in the mongoosejs.com documentation
Comments
IslandRhythms
added
the
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
label
Jan 31, 2024
const mongoose = require('mongoose');
const { Schema } = mongoose;
const productSchema = new Schema({
name: {
type: String,
required: true,
},
price: {
type: Number,
required: true,
min: 0,
},
}, {
timestamps: true
});
const ProductModel = mongoose.model('Product', productSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
const createdProduct = await ProductModel.create({ name: 'Product', price: 18 });
console.log('what is createdProduct', createdProduct);
const res = await ProductModel.replaceOne({ _id: createdProduct.id}, { name: 'Changed Product' });
console.log('what is res', res);
console.log(await ProductModel.findOne());
}
run(); |
This is expected behavior, but something we need to make more clear in the docs. Unfortunately MongoDB's For preserving schema.virtual('createdAt').get(function() {
return this._id.getTimestamp();
}); This approach works presuming you always use the default |
vkarpov15
added
docs
This issue is due to a mistake or omission in the mongoosejs.com documentation
and removed
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
labels
Feb 6, 2024
vkarpov15
added a commit
that referenced
this issue
Feb 7, 2024
docs(timestamps): clarify that `replaceOne()` and `findOneAndReplace()` overwrite timestamps
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prerequisites
Mongoose version
7.6.4
Node.js version
18.16.14
MongoDB server version
7.0.2
Typescript version (if applicable)
5.0.4
Description
When using
replaceOne
to update a document, only theupdatedAt
timestamp should be modified to reflect the time of the update. However, thecreatedAt
timestamp, which should remain constant to preserve the original creation time of the document, is also being updated to the same timestamp asupdatedAt
.The docs says:
So I assume that
createdAt
timestamp should not be updated when usingreplaceOne
.It should preserve the original
createdAt
timestamp of the document.Steps to Reproduce
Sorry for any inaccuracies, this is my first public issue.
Define a
ProductModel
:Now, let's create a document and try to replace with some updated data.
Expected Behavior
I expect the create to create a document with the following data:
While this is the expected replace result:
But this should be the actual replace behavior:
The text was updated successfully, but these errors were encountered: