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

fix: timestamps:false on bulkWrite works #13649

Merged
merged 4 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/helpers/model/castBulkWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const setDefaultsOnInsert = require('../setDefaultsOnInsert');

module.exports = function castBulkWrite(originalModel, op, options) {
const now = originalModel.base.now();

if (op['insertOne']) {
return (callback) => {
const model = decideModelByObject(originalModel, op['insertOne']['document']);
Expand Down Expand Up @@ -66,7 +65,9 @@ module.exports = function castBulkWrite(originalModel, op, options) {
applyTimestampsToUpdate(now, createdAt, updatedAt, op['updateOne']['update'], {});
}

applyTimestampsToChildren(now, op['updateOne']['update'], model.schema);
if (op['updateOne'].timestamps !== false) {
applyTimestampsToChildren(now, op['updateOne']['update'], model.schema);
}

if (op['updateOne'].setDefaultsOnInsert !== false) {
setDefaultsOnInsert(op['updateOne']['filter'], model.schema, op['updateOne']['update'], {
Expand Down Expand Up @@ -117,8 +118,9 @@ module.exports = function castBulkWrite(originalModel, op, options) {
const updatedAt = model.schema.$timestamps.updatedAt;
applyTimestampsToUpdate(now, createdAt, updatedAt, op['updateMany']['update'], {});
}

applyTimestampsToChildren(now, op['updateMany']['update'], model.schema);
if (op['updateMany'].timestamps !== false) {
applyTimestampsToChildren(now, op['updateMany']['update'], model.schema);
}

_addDiscriminatorToObject(schema, op['updateMany']['filter']);

Expand Down
46 changes: 46 additions & 0 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5732,6 +5732,52 @@ describe('Model', function() {

});

it('bulkwrite should not change updatedAt on subdocs when timestamps set to false (gh-13611)', async function() {

const postSchema = new Schema({
title: String,
category: String,
isDeleted: Boolean
}, { timestamps: true });

const userSchema = new Schema({
name: String,
isDeleted: Boolean,
posts: { type: [postSchema] }
}, { timestamps: true });

const User = db.model('gh13611User', userSchema);

const entry = await User.create({
name: 'Test Testerson',
posts: [{ title: 'title a', category: 'a', isDeleted: false }, { title: 'title b', category: 'b', isDeleted: false }],
isDeleted: false
});
const initialTime = entry.posts[0].updatedAt;
await delay(10);

await User.bulkWrite([{
updateMany: {
filter: {
isDeleted: false
},
update: {
'posts.$[post].isDeleted': true
},
arrayFilters: [
{
'post.category': { $eq: 'a' }
}
],
upsert: false,
timestamps: false
}
}]);
const res = await User.findOne({ _id: entry._id });
const currentTime = res.posts[0].updatedAt;
assert.equal(initialTime.getTime(), currentTime.getTime());
});

it('bulkWrite can overwrite schema `strict` option for filters and updates (gh-8778)', async function() {
// Arrange
const userSchema = new Schema({
Expand Down