Setting a query for MongoDB The following function sets a query for MongoDB, but that's not very important. It takes 2 arguments key can be either - which comes from an empty search by the user in the front end, or any other string. Select can be either sample or all
So it's a switch like this:
                            |
                          /   \
                         /\   /\

In my head at least.
But the code looks terribly bad, so something may be wrong, any tip or trick is welcome:
function setQuery(key, select){
  const project = {&quot;$project&quot;:{&quot;_id&quot;:0, &quot;quote&quot;:1} } 
  if (key===&quot;-&quot;){ 
   return select===&quot;sample&quot; 
      ? [{&quot;$sample&quot;:{&quot;size&quot;:5}}, project ]
      : [{$match:{quote:{$ne:null}}}, project] } else {
        //if key is not - (is a word)
        const reg = new RegExp(key, 'i') 
        const userText =  { &quot;$match&quot;:{&quot;quote&quot;:{ &quot;$regex&quot;: reg } }}
      return select===&quot;sample&quot;
        ? [userText, {&quot;$sample&quot;:{&quot;size&quot;:5}}, project]
        : [userText, project] 
    }
  }
I think the clearest way to approach this would be to define all the array items up front, to separate out the somewhat convoluted data boilerplate from the branch logic. Also, every query contains project at the end, so to decrease repetitiveness (and to increase the distinction between the different branches later), you might make a function for it.
function setQuery(key, select) {
  // Not sure what these should be called, there's probably a more precise name than `matchers`
  const matchers = {
    project: {$project:{_id:0, quote:1} },
    sample: {$sample:{size:5}},
    any: {$match:{quote:{$ne:null}}},
    text: { $match:{quote:{ $regex: new RegExp(key, 'i') } }},
  };
  const makeProjectQuery = (...initials) =&gt; [...initials, matchers.project];

  if (key === &quot;-&quot;) {
    return makeProjectQuery(select === &quot;sample&quot; ? matchers.sample : matchers.any);
  } else {
    // Then key is a word; search for it
    return select === &quot;sample&quot;
      ? makeProjectQuery(matchers.text, matchers.sample)
      : makeProjectQuery(matchers.text);
  }
}

It's a few more lines, but I think it's easier to make sense of at a glance. Plus, if you ever needed to expand the functionality of this section, it would be a pretty natural extension to add something to matchers and add conditions for it in the lower body.
The code could be further condensed, but it would come at the sake of readability.
Review
Some suggestions regarding your original code:
Only quote property names when necessary - leaving out the &quot;s results in less syntax noise and is a bit easier to read and write.
Indent code properly - two statements that are at the same level in a block should have the same indentation level - this makes the control flow clearer. Similarly, when ending a block, consider putting the } on the next line - being able to pick up on the locations where blocks begin and end at a glance is very important. Lots of IDEs can automatically indent code for you, they're well worth it.
Consider using spaces around operators, especially in complicated code. select === &quot;sample&quot; is easier to recognize at a glance than select===&quot;sample&quot; (especially if more operators become involved later)
When one branch differs from another only in a single expression, consider the conditional operator instead of repeating everything else around the expression. (eg, replace with something like select === &quot;sample&quot; ? matchers.sample : matchers.any as above) You don't have to use the conditional operator - sometimes it's clearer without it - but it's something to think about in that sort of situation.
Good comments use natural language that flows while reading the code out loud.