Is there a way to use the aggregate pipeline method for this query

前端 未结 1 1963
离开以前
离开以前 2021-01-29 04:22

I have a database of over 2000 reviews for products on an online store, and I am trying to find all of the users who have written minimum 5 reviews, and I would like to list the

1条回答
  •  故里飘歌
    2021-01-29 04:52

    I guess you need to run this aggregation:

    db.review.aggregate([
      {
        $group: {
          _id: "$SH_reviewerID",
          name: {
            $first: "$SH_reviewerName"
          },
          number: {
            $sum: 1
          }
        }
      },
      {
        $match: {
          number: {
            $gte: 5
          }
        }
      },
      //Optional - Order views descendant
      {
        $sort: {
          number: -1
        }
      },
      //Optional - Change field names
      {
        $project: {
          _id: 0,
          SH_reviewerID: "$_id",
          SH_reviewerName: "$name",
          SH_reviewerViews: "$number"
        }
      }
    ])
    

    MongoPlayground

    0 讨论(0)
提交回复
热议问题