【问题标题】:How To Count Values Inside Deeply Nested Array Of Objects in Mongodb Aggregation如何计算 Mongodb 聚合中深度嵌套对象数组中的值
【发布时间】:2023-01-12 17:54:46
【问题描述】:

我想对具有另一个对象数组的对象数组内的值求和。 就我而言;如何计算“iocs”数组下“urls”数组中所有文档中的“url”值;

Mongo游乐场:open

这是文档示例;

[
  {
    "_id": {
      "$oid": "63b4993d0625ebe8b6f5b06e"
    },
    "iocs": [
      {
        "urls": [
          {
            "url": "7.1.5.2",
            
          }
        ],
        
      },
      {
        "urls": [
          {
            "url": "https://l-ink.me/GeheimeBegierde",
            
          },
          {
            "url": "GeheimeBegierde.ch",
            
          }
        ],
        
      },
      {
        "urls": [
          {
            "url": "https://l-ink.me/GeheimeBegierde",
            
          }
        ],
        
      }
    ],
    type: "2"
  },
  {
    "_id": {
      "$oid": "63b4993d0624ebe8b6f5b06e"
    },
    "iocs": [
      {
        "urls": [
          {
            "url": "7.1.5.2",
            
          }
        ],
        
      },
      {
        "urls": [
          {
            "url": "https://l-ink.me/GeheimeBegierde",
            
          },
          {
            "url": "GeheimeBegierde.ch",
            
          }
        ],
        
      },
      {
        "urls": [
          {
            "url": "https://l-ink.me/GeheimeBegierde",
            
          }
        ],
        
      }
    ],
    type: "3"
  },
  {
    "_id": {
      "$oid": "63b4993d0615ebe8b6f5b06e"
    },
    "iocs": [
      {
        "urls": [
          {
            "url": "www.google.com",
            
          }
        ],
        
      },
      {
        "urls": [
          {
            "url": "abc.xyz",
            
          },
          {
            "url": "GeheimeBegierde.ch",
            
          }
        ],
        
      },
      {
        "urls": [
          {
            "url": "https://123.12",
            
          }
        ],
        
      }
    ],
    type: "1"
  }
]

预期输出是这样的;

url: "7.1.5.2",
count:2,
types:[2,3]

url: "https://l-ink.me/GeheimeBegierde",
count:4,
types:[2,3],

url: "abc.xyz",
count:1,
types:[1],

我试过 unwind iocs 然后投影 urls 但无法弄清楚如何获得此输出。我想我必须使用组,但如何? mongodb 新手。

任何帮助,将不胜感激。谢谢大家。

注意:所有答案都有效。谢谢大家的贡献。

【问题讨论】:

    标签: mongodb aggregate project group unwind


    【解决方案1】:

    你可以做这样的事情!

    db.collection.aggregate([
      {
        "$unwind": "$iocs"
      },
      {
        "$unwind": "$iocs.urls"
      },
      {
        "$group": {
          "_id": "$iocs.urls.url",
          "count": {
            "$sum": 1
          },
          "types": {
            "$addToSet": "$type"
          }
        }
      },
      {
        "$project": {
          url: "$_id",
          _id: 0,
          types: 1,
          count: 1
        }
      },
    ])
    

    https://mongoplayground.net/p/hhMqh2zI_SX

    【讨论】:

      【解决方案2】:

      这是您可以做到的一种方法。

      db.collection.aggregate([
        {"$unwind": "$iocs"},
        {"$unwind": "$iocs.urls"},
        {
          "$group": {
            "_id": "$iocs.urls.url",
            "count": {"$count": {}},
            "types": {"$addToSet": {"$toInt": "$type"}}
          }
        },
        {
          "$set": {
            "url": "$_id",
            "_id": "$$REMOVE"
          }
        }
      ])
      

      试试mongoplayground.net

      【讨论】:

        【解决方案3】:

        你可以试试这个查询:

        • $unwind解构嵌套数组。
        • 然后按url分组,使用$sum获取计数并将类型添加到集合中(避免重复,否则您可以简单地使用$push
        db.collection.aggregate([
          {
            "$unwind": "$iocs"
          },
          {
            "$unwind": "$iocs.urls"
          },
          {
            "$group": {
              "_id": "$iocs.urls.url",
              "count": {
                "$sum": 1
              },
              "types": {
                "$addToSet": "$type"
              }
            }
          }
        ])
        

        例子here

        【讨论】:

          【解决方案4】:

          由于 $unwind 被认为是低效操作,另一种选择是使用 $reduce 并且只使用 $unwind 一次:

          db.collection.aggregate([
            {$project: {
                type: 1,
                urls: {
                  $reduce: {
                    input: "$iocs",
                    initialValue: [],
                    in: {$concatArrays: ["$$value", "$$this.urls.url"]}
                  }
                }
            }},
            {$unwind: "$urls"},
            {$group: {
                _id: "$urls",
                type: {$addToSet: "$type"},
                count: {$sum: 1}
            }},
            {$project: {url: "$_id", count: 1, type: 1}}
          ])
          

          查看它在playground example 上的工作原理

          【讨论】:

            猜你喜欢
            • 2021-01-18
            • 2015-03-10
            • 1970-01-01
            • 1970-01-01
            • 2018-09-10
            • 2018-11-17
            • 2021-09-22
            • 2019-05-05
            • 2021-08-13
            相关资源
            最近更新 更多