如何使用mongodb聚合计算数组字段上的百分比?

Mustafa Shaikh

我有以下文件

users = [
  {
    type: 'A',
    name: 'anil',
    logins: [
      { at: '2-3-2019', device: 'mobile' },
      { at: '3-3-2019', device: 'desktop' },
      { at: '4-3-2019', device: 'tab' },
      { at: '5-3-2019', device: 'mobile' }
    ]
  },
  {
    type: 'A',
    name: 'rakesh',
    logins: [
      { at: '2-3-2019', device: 'desktop' },
      { at: '3-3-2019', device: 'mobile' },
      { at: '4-3-2019', device: 'desktop' },
      { at: '5-3-2019', device: 'tab' }
    ]
  },
  {
    type: 'A',
    name: 'rahul',
    logins: [
      { at: '2-3-2019' device: 'tab' },
      { at: '3-3-2019' device: 'mobile' },
      { at: '4-3-2019' device: 'tab' },
      { at: '5-3-2019' device: 'tab' }
    ]
  }
]

我需要计算每个用户使用的设备类型“ A”的百分比。

如果我们查看用户anil的设备使用情况,

mobile: 50%
desktop: 25%
tab: 25%

使用率最高的是50%使用率的移动设备,因此应将其视为移动设备。就像上面的最终输出一样,

[
  {
    name: 'anil',
    device: 'mobile',
    logins: 50%
  },
  {
    name: 'rakesh',
    device: 'desktop',
    logins: 50%
  },
  {
    name: 'rahul',
    device: 'tab',
    logins: 75%
  }
]

谢谢你的帮助。

阿什

您可以使用以下汇总

这里的整体逻辑是在数组内查找重复项,之后您只需要$maplogins数组上执行以下操作即可使用公式计算设备的百分比

(numberOfDevices * 100) / total size of the array

如果您从以下汇总中一步一步地删除它来理解它会更好。

db.collection.aggregate([
  { "$match": { "type": "A" }},
  { "$addFields": {
    "logins": {
      "$arrayToObject": {
        "$map": {
          "input": { "$setUnion": ["$logins.device"] },
          "as": "m",
          "in": {
            "k": "$$m",
            "v": {
              "$divide": [
                {
                  "$multiply": [
                    { "$size": {
                      "$filter": {
                        "input": "$logins",
                        "as": "d",
                        "cond": {
                          "$eq": ["$$d.device", "$$m"]
                        }
                      }
                    }},
                    100
                  ]
                },
                { "$size": "$logins" }
              ]
            }
          }
        }
      }
    }
  }}
])

蒙哥运动场

[
  {
    "_id": ObjectId("5a934e000102030405000000"),
    "logins": {
      "desktop": 25,
      "mobile": 50,
      "tab": 25
    },
    "name": "anil",
    "type": "A"
  },
  {
    "_id": ObjectId("5a934e000102030405000001"),
    "logins": {
      "desktop": 50,
      "mobile": 25,
      "tab": 25
    },
    "name": "rakesh",
    "type": "A"
  },
  {
    "_id": ObjectId("5a934e000102030405000002"),
    "logins": {
      "mobile": 25,
      "tab": 75
    },
    "name": "rahul",
    "type": "A"
  }
]

确切的输出->在$max获得所有设备的百分比之后,我刚刚从对象数组中找到了元素。

db.collection.aggregate([
  { "$match": { "type": "A" }},
  { "$addFields": {
    "logins": {
      "$map": {
        "input": { "$setUnion": ["$logins.device"] },
        "as": "m",
        "in": {
          "k": "$$m",
          "v": {
            "$divide": [
              {
                "$multiply": [
                  { "$size": {
                    "$filter": {
                      "input": "$logins",
                      "as": "d",
                      "cond": {
                        "$eq": ["$$d.device", "$$m"]
                      }
                    }
                  }},
                  100
                ]
              },
              { "$size": "$logins" }
            ]
          }
        }
      }
    }
  }},
  { "$replaceRoot": {
    "newRoot": {
      "$mergeObjects": [
        "$$ROOT",
        {
          "$arrayElemAt": [
            "$logins",
            {
              "$indexOfArray": [
                "$logins.v",
                { "$max": "$logins.v" }
              ]
            }
          ]
        }
      ]
    }
  }},
  { "$project": { "logins": 0 }}
])

蒙哥运动场

[
  {
    "_id": ObjectId("5a934e000102030405000000"),
    "k": "mobile",
    "name": "anil",
    "type": "A",
    "v": 50
  },
  {
    "_id": ObjectId("5a934e000102030405000001"),
    "k": "desktop",
    "name": "rakesh",
    "type": "A",
    "v": 50
  },
  {
    "_id": ObjectId("5a934e000102030405000002"),
    "k": "tab",
    "name": "rahul",
    "type": "A",
    "v": 75
  }
]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章