0

Is it possible to use $nin and $in on the variable basis in mongoose find parameters?

Like if suppose am getting req.params.take = True then I want the parameter check to be

Document.find({_id : {
  $in:[PreDefinedArray]
}}

or else if it's not present I want it to be

Document.find({_id : {
  $nin:[PreDefinedArray2]
}}

Not this can be for many populate statements using a match, so I cannot use if else or any other conditioning statement for the same to have multiple types of queries. Can it be done with some other technique? As I tried replacing $in and $nin with string variables like this

var selector = (check) ? ("$in") : ("$nin");

Document.find({_id : {
  selector : [PreDefinedArray2]
}}

but it didn't work.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Soumyajit Dutta
  • 198
  • 2
  • 15

1 Answers1

0

Use the condition statement in mongoose:

 { 
  $cond: 
   { 
    if: <boolean-expression>, then: <true-case>,
    else: <false-case-> 
   } 
 }
Wisely D Cruizer
  • 916
  • 9
  • 23