通过过滤另一个集合进行分组

时间:2017-05-24 06:40:24

标签: mongodb meteor aggregation-framework

我使用 Meteor MongoDB 。我需要使用聚合发布(我使用 jcbernack:reactive-aggregate ReactiveAggregate )。

db.getCollection('Jobs').aggregate([ 
{
  $lookup:
    {
      from: "JobMatches",
      localField: "_id",
      foreignField: "jobId",
      as: "matches"
    }
},
{ $project: 
  { 
    matches: { 
        '$filter': { 
            input: '$matches', 
            as: 'match', 
            cond: { '$and': [{$eq: ['$$match.userId', userId]}]}
        }
    }
   }
},
{$match:  { 'matches.score': { '$gte': 60 }},
{$sort: { "matches.score": -1 }},
{$limit: 6}

])

在客户端,我只获得数据部分(限制6)。所以我将不得不计算服务器端所有数据的数量。我无法使用find().count(),因为在没有聚合的find()调用中,我无法使用与其他集合关联的过滤器(例如此{ 'matches.score': { '$gte': 60 })。如何计算以这种方式过滤的数据?可能需要在管道中使用 $ group

0 个答案:

没有答案