【问题标题】:How to return documents that has an array contains more than one objects having similar field如何返回具有数组的文档包含多个具有相似字段的对象
【发布时间】:2020-11-05 05:23:58
【问题描述】:

我有这样的查询

const papers = await Paper.aggregate([
                    { $match: filter },
                    {
                        $lookup: {
                            "from": "reviews",
                            "localField": "review",
                            "foreignField": "_id",
                            "as": "review"
                        }
                    },
                    { $unwind: "$review" },
                    { $match: { "review.reviews.reviewStatus": "SUBMITTED" } },
                ])

返回类似的结果

[
  {
    _id: 5f721ca3909430bf5bf38133,
    manuscript: {
      keywords: [Array],
      title: 'Viverra aliquet eget sit amet tellus cras adipiscing enim. ',
      abstract: 'Ultricies leo integer',
      authors: [Array],
      fundingInfo: ''
    },
    review: {
      _id: 5f721ca5909430bf5bf38138,
      reviews: [Array]
    }
  }
]

review 对象内的 reviews 数组包含不同的审稿人对这篇论文的审阅。 reviewStatus 会根据评论者的评论进行更改。

{
  _id: 5f721ca5909430bf5bf38138,
  reviews: [
    {
      invitationStatus: 'ACCEPTED',
      reviewType: 'MINOR_REVISION',
      reviewStatus: 'SUBMITTED',
      _id: 5f9d2fa9909430bf5bf3814a
    },
    {
      invitationStatus: 'PENDING',
      reviewType: 'MAJOR_REVISION',
      reviewStatus: 'SAVED',
      _id: 5f9d38223357c83460cb77df
    },
    {
      invitationStatus: 'ACCEPTED',
      reviewType: 'MINOR_REVISION',
      reviewStatus: 'SUBMITTED',
      _id: 5f9d39af3357c83460cb77e2,
    }
  ]
}

现在,我只想获取那些具有两个或多个 reviewStatusSUBMITTED

的论文

【问题讨论】:

    标签: node.js mongodb mongoose aggregation-framework


    【解决方案1】:

    您可以在$lookup 之后的$match 阶段内使用$expr 运算符,这将过滤带有reviews 数组的纸质文档,其中包含 至少提交了两个。以下操作显示了您如何处理管道:

    const papers = await Paper.aggregate([
        { '$match': filter },
        {
            '$lookup': {
                "from": "reviews",
                "localField": "review",
                "foreignField": "_id",
                "as": "review"
            }
        },
        { '$addFields': { 
            'review': { '$arrayElemAt': ['$review', 0] }
        } },
        {  '$match': {
            '$expr': {
                '$gt': [
                    { '$size': {
                        '$filter': {
                            'input': '$review.reviews',
                            'cond': { '$eq': ['$$this.reviewStatus', "SUBMITTED"] }
                        }
                    } },
                    1
                ]
            } 
        } }
    ])
    

    【讨论】:

    • 非常感谢您的真诚合作。但是在添加你上面写的 $match 之后,我得到了一个空数组。我不确定为什么。请帮忙解决这个问题?
    • 你做得很好,你的答案几乎是正确的。我只需要 $unwind 审查并更改 'input' 之类的 'input': '$review.reviews'
    • @FozleRabbiShafi 我不太确定 reviews 集合的架构是什么样的,但从你上面的评论中我发现它有一个评论字段,它是一个数组
    • 你真是个了不起的人。再次非常感谢。
    猜你喜欢
    • 2021-09-30
    • 2022-12-03
    • 1970-01-01
    • 1970-01-01
    • 2020-02-25
    • 1970-01-01
    • 2019-11-07
    • 1970-01-01
    • 2016-06-18
    相关资源
    最近更新 更多