我正在尝试改进我的mongodb聚合查询,并想尝试在我的$ lookup字段中使用userId变量,这样我就可以确定用户是否在使用$ matching阶段之前查看了一段内容 . 目标是确保在匹配中没有返回用户已经看过的任何文章 .

目前我可以在$ match阶段后查找特定文章,然后重新搜索该文章是否在该用户的'readNews'集合中,但我更愿意知道用户通过$ lookup查看了哪些文章然后删除那些来自可能的比赛返回 .

这可能吗?我知道在运行aggreagtion之前我可以进行单独的查询以获取我想要的信息, but is it more efficient ( or even possible) to do this within the same aggregate query? Something Like

//Phase 1 Pipeline look in seperate collection
$lookup: {
           from: 'readArticles',
           localField: '$$userId variable',
           foreignField: '_id',
           as: 'userReadArticles'
           }
//phase 2 take the id's of the readArticles and exclude from match
$match: {
          id: { $ne : userReadArticles}
        }