Answer a question

I run a query that return an array of users through an aggregation. In order not to return the document of the user performing the query, I added a $filter on his id. But I receive the following error:

$filter is not allowed in this atlas tier.

I am connected to a free MongoAtlas cluster. Here is my code.

 User.aggregate([
        { $filter: { _id: { $eq: id } } },
        {
          $match: {
            jobs: { $in: jobs },
           age: { $gte: minAge, $lte: maxAge },
          },
        },
      ]).exec((_, data) => res.json({ data }));

How to fix this?

Answers

$filter is not a pipeline stage, it's for filtering an array expression. You can just put what you are trying to filter in the $match stage.

 User.aggregate([
        {
          $match: {
            _id: id,
            jobs: { $in: jobs },
            age: { $gte: minAge, $lte: maxAge },
          },
        },
      ]).exec((_, data) => res.json({ data }));
Logo

MongoDB社区为您提供最前沿的新闻资讯和知识内容

更多推荐