I am making a group chat conversation schema using mongoose but I have been having trouble with a query for the past few days. My goal here is to find all the unread conversations of a user.
My schema looks like this:
const ConversationSchema = new Schema(
{
participants: [{
user: mongoose.Schema.Types.ObjectId,
lastViewed: Date, // the last date the user checked the conversation
}],
updatedAt: Date,
}
);
And this is the simplified sample data:
{
participants: [
{
user: 'A',
lastViewed: 'Jan 11'
},
{
user: 'B',
lastViewed: 'Jan 13'
},
],
updatedAt: 'Jan 13',
}
For me, a conversation would be considered unread for a user if:
- the user is in the participant array
- and the conversation
updatedAtvalue is greater than the participant'slastViewedvalue
Lastly, my mongoose find function looks like this
const conversations = await conversationModel.find({
'participants.user': user.id,
$expr: {
$gt: ['updatedAt', 'participants.user.lastViewed'],
},
});
So when I try to query for user B's unread conversations, I should be expecting an empty [] result as Jan 13 = Jan 13 but I am still getting the conversation in the results. I think the problem is in the $expr argument but I am unsure what to add as it deals with an array. Any help would be greatly appreciated.