Setting a query for MongoDB
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.