Search code examples
mongodbmongodb-querymongodb-compass

Syntax in $or statement


enter image description here

I have the following filter which I am trying to test in MongoDB Compass:

{$or: ["OwedTaxes": {$regex: "$"},"OwedTaxes": {$exists: false}]}

Meaning the OwedTaxes field contains a "$" sign or does not exist.

There is a syntax error but I don't understand what it is. What am I doing wrong?


Solution

  • You need to wrap each element in the array with { } curly brace to represent a valid BSON document.

    {
      $or: [
        { "OwedTaxes": {$regex: "$"} },
        { "OwedTaxes": {$exists: false} }
      ]
    }