7

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",

skyboyer
  • 22,209
  • 7
  • 57
  • 64
Nidor
  • 85
  • 1
  • 5

1 Answers1

12

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

hhharsha36
  • 3,089
  • 2
  • 12
  • 12