The problem is with the second object in the $and statement, If isDraft is false match the document otherwise check topicCreator
{
$match: {
$and: [
{ topicTarget: new mongoose.Types.ObjectId(topicTarget), topicType: params?.topicType },
{
$or: [
{ "isDraft": false },
{ "topicCreator": new mongoose.Types.ObjectId(topicCreator) }
]
},
{
$or: [
{ isDeleted: { $exists: false }},
{ $and: [
{isDeleted: { $exists: true }},
{isDeleted: false}
]}
]
}
]
}
},```
I've tried making the second part of the $or into and $and to specify isDraft: true but that doesnt seem to do anything either