Skip to content

Commit c6a646c

Browse files
committedJul 16, 2021
test(timestamps): repro #10447
1 parent 169f9e1 commit c6a646c

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
 

‎test/timestamps.test.js

+44
Original file line numberDiff line numberDiff line change
@@ -875,4 +875,48 @@ describe('timestamps', function() {
875875
assert.ok(fromDb.content.a.updatedAt.valueOf() > ts.valueOf());
876876
});
877877
});
878+
879+
it('sets createdAt when using $push/$addToSet on path with positional operator (gh-10447)', function() {
880+
const userSchema = new Schema({
881+
email: String
882+
}, { timestamps: true });
883+
const eventSchema = new Schema({
884+
users: [userSchema],
885+
message: String
886+
}, { timestamps: true });
887+
888+
const churchSchema = new Schema({
889+
events: [eventSchema]
890+
}, { timestamps: true });
891+
892+
const Church = db.model('Test', churchSchema);
893+
894+
return co(function*() {
895+
yield Church.create({
896+
events: [{
897+
churchId: 1,
898+
message: 'test',
899+
users: [{
900+
churchId: 1,
901+
email: 'test@google.com'
902+
}]
903+
}]
904+
});
905+
906+
const church = yield Church.findOneAndUpdate({
907+
events: { $elemMatch: { users: { $not: { $elemMatch: { email: 'test2@google.com' } } } } }
908+
}, {
909+
$addToSet: {
910+
'events.$.users': { churchId: 1, email: 'test2@google.com' }
911+
}
912+
}, {
913+
new: true
914+
});
915+
916+
assert.equal(church.events.length, 1);
917+
assert.equal(church.events[0].users.length, 2);
918+
assert.equal(church.events[0].users[1].email, 'test2@google.com');
919+
assert.ok(church.events[0].users[1].createdAt);
920+
});
921+
});
878922
});

0 commit comments

Comments
 (0)
Please sign in to comment.