【问题标题】:Like Count Query MongooseLike Count 查询 Mongoose
【发布时间】:2021-11-06 11:54:03
【问题描述】:

我在猫鼬集合 A 和集合 B 中有两个集合

集合 A(用户):

{
 id : 1,
 Name : User1,
},
{
  id: 2,
  Name :  User2
},
{
 id: 3,
 Name: User3
}

collection B(Item) :
{
 id :1,
 name : Item1
},
{
 id: 2,
 name: Item2
}


Collection C(ItemLikes)
{
 id : 11,
 isLike : true,
 userId : 1,
 itemId : 1,
}
{
 id:12,
 isLike : true,
 userId : 2,
 ItemId : 1
},
{
 id:13,
 isLike : false,
 userId : 3,
 ItemId : 1
},
{
 id:14,
 isLike : false,
 userId : 3,
 ItemId : 2
}

现在我需要以下输出

 {
   ItemId : 1,
   totalLikes(true) :  2,
   isYourLike : true,
   userId: 1
 }

【问题讨论】:

  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: mongodb mongoose mongodb-query aggregation-framework


【解决方案1】:

如果您只想调查第 1 项,请使用 $group

  • $match发现itemId是1
  • $group 按项目 1 分组并在其中使用条件总和
  • $projectisYourLike 更改为布尔值
db.itemLikes.aggregate([
  {
    "$match": {
      "itemId": 1
    },
    
  },
  {
    $group: {
      "_id": "$itemId",
      "isYourLike": {
        $sum: {
          $cond: [
            {
              $and: [
                {
                  $eq: [
                    "$isLike",
                    true
                  ]
                },
                {
                  $eq: [
                    "$userId",
                    1
                  ]
                }
              ]
            },
            1,
            0
          ]
        }
      },
      "totalLikes(true)": {
        $sum: {
          $cond: [
            {
              $eq: [
                "$isLike",
                true
              ]
            },
            1,
            0
          ]
        }
      }
    }
  },
  {
    "$project": {
      "itemId": "$_id",
      "isYourLike": {
        $cond: {
          if: {
            $gte: [
              "$isYourLike",
              1
            ]
          },
          then: true,
          else: false
        }
      },
      "totalLikes(true)": 1
    }
  }
])

示例:mongoplayground


如果您想计算每个项目,请使用 $lookup

  • $lookup加入不同的收藏
  • $project里面的条件总和
  • $projectisYourLike 更改为布尔值
db.item.aggregate([
  {
    "$lookup": {
      "from": "itemLikes",
      "localField": "id",
      "foreignField": "itemId",
      "as": "likes"
    }
  },
  {
    "$project": {
      "itemId": "$id",
      "totalLikes(true)": {
        "$size": {
          "$filter": {
            "input": "$likes",
            "cond": {
              "$eq": [
                "$$this.isLike",
                true
              ]
            }
          }
        }
      },
      "isYourLike": {
        "$size": {
          "$filter": {
            "input": "$likes",
            "cond": {
              $and: [
                {
                  $eq: [
                    "$$this.isLike",
                    true
                  ]
                },
                {
                  $eq: [
                    "$$this.userId",
                    1
                  ]
                }
              ]
            }
          }
        }
      },
      
    }
  },
  {
    "$project": {
      "itemId": "$itemId",
      "isYourLike": {
        $cond: {
          if: {
            $gte: [
              "$isYourLike",
              1
            ]
          },
          then: true,
          else: false
        }
      },
      "totalLikes(true)": 1
    }
  }
])

示例:mongoplayground

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-23
    • 2017-06-03
    • 1970-01-01
    • 2021-02-20
    • 2010-11-26
    • 1970-01-01
    • 2021-04-22
    • 1970-01-01
    相关资源
    最近更新 更多