Hello Dear Mongodb Community , (schema attached below) I am working on a chat application with the following requirement ; how to query for inbox documents where user1 is a participant & the matching document that has a deleted_by object inside that belongs to user1 has also have the deleted_at date less than inbox.last_activity_date , now the functionality is to bring inboxes for a user where they deleted it , and when a new activity happens after than they deleted the document it must still return , like imagine in whatsapp you delete a chat and when there is a new message there (new activity) , you will still retrieve it since the date you deleted was before the latest even happening. Now when I try the following query (removed the part that matches the user_id since its not the problem) I get the error:
`
Cast to date failed for value “$last_activity_date” (type string) at path “deleted_at” for model “Inbox”
``
now am I not referencing the inbox.last_activity_date correctly here ? since its type date and the actual document saved in db has also type date and I use strict typing so I dont think the casting or field type is the problem nevertheless I can’t figure it out.
Preferably , I do not want to use aggregation pipeline in the code and more elegant approach than mine is always welcome…
I’d appreciate your help , thank you.
const inboxes = await Inbox.find({ 'deleted_by.deleted_at': { $lt: '$last_activity_date' } })
const inboxSchema = new mongoose.Schema({
participants: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
}],
last_activity_date: {
type: Date,
default: Date.now
},
deleted_by: [{
user: { type: mongoose.Types.ObjectId, ref: 'User' },
deleted_at: { type: Date, default: Date.now }
}],
},{ strict: true });