I'm building a chat application with Mongoose and I have two Models:
Conversation: { participants [user1,user2], updatedAt, createdAt }
AND
Message: { conversationId:, content:'' ... }
I'm using paginate to retrieve 20 documents from Conversation with the latest sent messages
let conversations = await Conversation.paginate(
{participants: userID},
{
//select: '_id participants updatedAt createdAt',
populate: {
path: 'participants',
select: 'name lastName _id photoURL'
},
page: page,
limit: 20,
sort: {updatedAt: -1}
}
);
When a new message is sent a new Message Document is created. However this will not update the Conversation "updatedAt" value.
In order for the code above to work I need to update the updatedAt value in the Conversation document every time a new message is sent.
My Question:
Is there a performance-light way to update the updatedAt value in the Conversation document every time a message document is created?