2 / 3
Jun 2024

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

Please provide sample documents that matches and does not match the cases you want.

Also share everything else that you tried so we can avoid working in a direction you know does not work. It might also make us better understand the use case.

Hi Dylan

A fallback condition can be achieved using a $switch statement. Replace the

condition with an expression of switch. I am not sure of the exact mongoose statement, however, you would do something like this:

$expr: { $switch: { branches: [ { case: { $eq:["$isDraft", false] }, then: true }], default: { $eq: ["$topicCreator", new mongoose.Types.ObjectId(topicCreator)] } } }

This is similar to SELECT CASE WHEN in SQL.

Hope that helps

Craig.