I'm struggling with implementing Mongo Queries, particularly in relation to a specific section of code.
The problem arises when attempting to perform a query that involves checking a variable and then making a match.
const FIRST_SUBJECT = keywords_list[0].subjects || {"$ne":null};
.
.
.
{'$match': {'entities.qcode': FIRST_SUBJECT}},
In this scenario, I only want to execute the $match if FIRST_SUBJECT
is not empty or null.
I have experimented with using a $cond
within the $match
, as well as considering other approaches, but encountered errors in both cases.
To put it simply:
if(FIRST_SUBJECT)
$match
else
skip $match
Can anyone provide guidance on how to achieve this effectively?