Home > Net >  Unrecognized pipeline stage name: '$filter' on mongodb 6.0
Unrecognized pipeline stage name: '$filter' on mongodb 6.0

Time:09-13

I'm getting a strange error, that $filter is not recognized on mongodb v6.0. As the documentation says, it was introduced in v3.2 I copied the first example from the documentation for testing, but the error also occurs with any other aggregation pipeline that includes a $filter stage.

rs0 [primary] test> db.users.aggregate([
   {
      $filter: {
         input: [1, "a", 2, null, 3.1, NumberLong(4), "5"],
         as: "num",
         cond: { $and: [{ $gte: ["$$num", NumberLong("-9223372036854775807")] }, { $lte: ["$$num", NumberLong("9223372036854775807")] }] }
      }
   }
])

Warning: NumberLong: specifying a number as argument is deprecated and may lead to loss of precision, pass a string instead

MongoServerError: Unrecognized pipeline stage name: '$filter'

These are the exact versions I'm using:

Using MongoDB:      6.0.1
Using Mongosh:      1.5.4

Does someone experienced something similar or have a clue what's going on and how to fix it?

CodePudding user response:

$filter should be wrapped in $project pipeline stage

{
  $project: {
    $filter: {
      input: [1, "a", 2, null, 3.1, NumberLong(4), "5"],
        as: "num",
          cond: {
            $and: [{
              $gte: ["$$num", NumberLong("-9223372036854775807")]
            }, {
              $lte: ["$$num", NumberLong("9223372036854775807")]
            }]
          }
    }
  }
}
  • Related