Search code examples
mongodbaggregation-frameworkmongodb-compass

Expected "[" or AggregationStage but "{" found.: MongoDB aggregation doesn't let me use $toLower inside a $eq


I wrote a MongoDB pipeline that has this code in it:

  {
    $eq: [
      {
        "$toLower": "HELLO"
      },
      "hello"
    ]
  }

And here's a screenshot of it in Mongo Compass

mongo compass

I am expecting it to simply return true, and "$match" everything (for now). Eventually I will swap "HELLO" with a field name etc.

Does anyone know why I am getting this error?


Solution

  • $match does not accept raw aggregation expressions. Instead, use a $expr query expression to include aggregation expression in $match.

    https://docs.mongodb.com/manual/reference/operator/aggregation/match/index.html#pipe._S_match

    $expr: {
        $eq: [
          {
            $toLower: "HELLO"
          },
          "hello"
        ]
    }
    

    Aggregate command Find method