2

$cond in mongodb is defined like this:

$cond: { if: { $gte: [ "$qty", 250 ] }, then: 30, else: 20 }

But, can we do that for a string ? I want to compare string value inside a document with regex, so something like this can work:

$cond: { if: { $match: [/sam/, "$name"] }, then: 30, else: 20 }

/sam/ is a regular expression and "$name" is the field that I want to compare to. As a side note, this expression will be put in the aggregate pipeline.

DAXaholic
  • 33,312
  • 6
  • 76
  • 74
DennyHiu
  • 4,861
  • 8
  • 48
  • 80

1 Answers1

1

No that is currently not possible and is an open feature request for quite a while.
Depending on your needs you may be able to do your stuff with $substr

DAXaholic
  • 33,312
  • 6
  • 76
  • 74