簡體   English   中英

如何計算 Mongodb 聚合中深度嵌套對象數組中的值

[英]How To Count Values Inside Deeply Nested Array Of Objects in Mongodb Aggregation

我想對具有另一個對象數組的對象數組內的值求和。 就我而言; 如何計算“iocs”數組下“urls”數組中所有文檔中的“url”值;

Mongo游樂場:開放

這是文檔示例;

[
  {
    "_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"
  }
]

預計 output 會像;

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 但無法弄清楚如何獲得這個 output。我想我必須使用組但是如何? 新手mongodb。

任何幫助,將不勝感激。 謝謝大家。

注意:所有答案都有效。 謝謝大家的貢獻。

這是您可以做到的一種方法。

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上試用。

你可以試試這個查詢:

  • $unwind解構嵌套數組。
  • 然后按url分組使用$sum獲取計數並將類型添加到一個集合中(以避免重復,否則您可以簡單地使用$push
db.collection.aggregate([
  {
    "$unwind": "$iocs"
  },
  {
    "$unwind": "$iocs.urls"
  },
  {
    "$group": {
      "_id": "$iocs.urls.url",
      "count": {
        "$sum": 1
      },
      "types": {
        "$addToSet": "$type"
      }
    }
  }
])

這里的例子

你可以做這樣的事情!

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

由於$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 示例中查看它是如何工作的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM