I have Mongoose model updated and then retrieved with $inc
operator that implements simple view counter:
const profile = await Profile.findOneAndUpdate({ userName }, { $inc: { viewsCount: 1 } });
Profile
schema has timestamps
option enabled. The problem is that updatedAt
is updated during viewsCount
update, this is not a desirable behaviour.
I would like to to disable updatedAt
updates when viewsCount
is updated, preferably by doing as few queries as possible.
I assume that Mongoose timestamps are implemented with pre-hooks.
How can findOneAndUpdate
increment viewsCount
without updating updatedAt
?
Can this be done by negating the effect of timestamp hook?
There are similar questions that address the problem with updatedAt
updates but solutions don't suit the case.