Cannot test mongo aggregation MongoError: Unrecognized pipeline stage name: '$set'

Viewed 3532

Im using mongo aggregation with mongoose. It works fine, but fails when run in jest.

I get the error:

MongoError: Unrecognized pipeline stage name: '$set'

when using $set inside an aggregation

$set: {
            isUsersContent: {
              $eq: ["$teacher._id", user._id],
            },
      }

"mongoose": "^5.3.12", "jest": "^25.1.0",

1 Answers

The $set MongoDB Aggregation pipeline stage will work only for MongoDB versions 4.2 and above.

You can use the $addFields Pipeline stage instead.

$addFields: {
            isUsersContent: {
              $eq: ["$teacher._id", user._id],
            },
      }

Note: The $set Aggregation Pipeline stage is nothing but an alias $addFields stage

Related