mongodb嵌套分组和单独的数组

发布于 2025-02-06 08:20:25 字数 1659 浏览 1 评论 0原文

我在下面有此示例BSON文件,我正在寻找每个交通警察的报告总数,对于每个交通警察,我想列出tpidnumtpname和 报告总数makereport。 我正在尝试实现这样的目标:

{ "_id" : { "Name" : "Kevin Brown", ID: "KBB001"}, "Total Report" : 2},
{ "_id" : { "Name" : "Thomas Moore", ID: "TM001"}, "Total Report" : 1}

这是我到目前为止所得到的,但它一直将tpidnumtpname这样。

{ 
  "_id" : [ "KB001", "TM001" ],
  "Total" : [ 
    { "Name" : [ "Kevin Brown", "Thomas Moore" ], 
      "ID" : [ "KB001", "TM001" ], 
      "Total Reports" : 2 
    } 
  ] 
}

db.trafficReport.aggregate({$group:{_id:"$TRAFFIC-POLICE.tpIdNum","Total":{$addToSet:{"Name": "$TRAFFIC-POLICE.tpName", "ID":"$TRAFFIC-POLICE.tpIdNum","Total Reports":{$size:"$TRAFFIC-POLICE.makeReport.reportId"}}}}})

  db.trafficReport.insert({
"TRAFFIC-POLICE": [{
    "tpIdNum": "KB001",
    "tpName": "Kevin Brown",
    "makeReport": [{
        "reportId": "TO-001",
        "reportDate": "7-October-2021",
        "hasViolation": [
            { "violationId": "VID-001" }]
    },
    {
        "reportId": "TO-002",
        "reportDate": "12-November-2021",
        "hasViolation": [
            { "violationId": "VID-002" }]
    }
    ]
},
{
    "tpIdNum": "TM001",
    "tpName": "Thomas Moore",
    "makeReport": [{
        "reportId": "TO-003",
        "reportDate": "7-October-2021",
        "hasViolation": [
            { "violationId": "VID-002" }]
    }
    ]
}],
})

I have this sample BSON document below, I am tring to find the total number of reports made by each traffic police and for each traffic police i want to list the tpIdNum, tpName, and the
total number of reports makeReport made.
I am trying to achieve something like this:

{ "_id" : { "Name" : "Kevin Brown", ID: "KBB001"}, "Total Report" : 2},
{ "_id" : { "Name" : "Thomas Moore", ID: "TM001"}, "Total Report" : 1}

This is what I got so far but it keeps putting the tpIdNum and tpName together like this:

{ 
  "_id" : [ "KB001", "TM001" ],
  "Total" : [ 
    { "Name" : [ "Kevin Brown", "Thomas Moore" ], 
      "ID" : [ "KB001", "TM001" ], 
      "Total Reports" : 2 
    } 
  ] 
}

db.trafficReport.aggregate({$group:{_id:"$TRAFFIC-POLICE.tpIdNum","Total":{$addToSet:{"Name": "$TRAFFIC-POLICE.tpName", "ID":"$TRAFFIC-POLICE.tpIdNum","Total Reports":{$size:"$TRAFFIC-POLICE.makeReport.reportId"}}}}})

  db.trafficReport.insert({
"TRAFFIC-POLICE": [{
    "tpIdNum": "KB001",
    "tpName": "Kevin Brown",
    "makeReport": [{
        "reportId": "TO-001",
        "reportDate": "7-October-2021",
        "hasViolation": [
            { "violationId": "VID-001" }]
    },
    {
        "reportId": "TO-002",
        "reportDate": "12-November-2021",
        "hasViolation": [
            { "violationId": "VID-002" }]
    }
    ]
},
{
    "tpIdNum": "TM001",
    "tpName": "Thomas Moore",
    "makeReport": [{
        "reportId": "TO-003",
        "reportDate": "7-October-2021",
        "hasViolation": [
            { "violationId": "VID-002" }]
    }
    ]
}],
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟火散人牵绊 2025-02-13 08:20:25

由于流量 - Police是一个数组,因此您需要$ MAP才能格式化每个项目。 $ group如果每个警察是一个单独的文档,将有所帮助。
因此,如果您希望它们在一个文档中(如下):

db.collection.aggregate([
  {
    $project: {
      _id: 0,
      Total: {
        $map: {
          input: "$TRAFFIC-POLICE",
          as: "pc",
          in: {
            _id: {ID: "$pc.tpIdNum", Name: "$pc.tpName"},
            "Total Report": {$size: "$pc.makeReport"}
          }
        }
      }
    }
  }
])

查看如何在

或,如果要单独的文档,请在$ $ group之前使用$ undind

db.collection.aggregate([
  {$unwind: "$TRAFFIC-POLICE"},
  {
    $group: {
      _id: "$TRAFFIC-POLICE.tpIdNum",
      Total: {
        $addToSet: {
          Name: "$TRAFFIC-POLICE.tpName",
          ID: "$TRAFFIC-POLICE.tpIdNum"
        }
      },
      "Total Reports": {
        $first: {$size: "$TRAFFIC-POLICE.makeReport"}
      }
    }
  },
  {$unset: "_id"}
])

请参阅游乐场示例 - 单独的文档

Since TRAFFIC-POLICE is an array, you need $map to format each item. $group would help if each policeman is a separate document.
So, if you want them in one document as they are:

db.collection.aggregate([
  {
    $project: {
      _id: 0,
      Total: {
        $map: {
          input: "$TRAFFIC-POLICE",
          as: "pc",
          in: {
            _id: {ID: "$pc.tpIdNum", Name: "$pc.tpName"},
            "Total Report": {$size: "$pc.makeReport"}
          }
        }
      }
    }
  }
])

See how it works on the playground example - one document

Or, if you want separate documents, use $unwind before the $group:

db.collection.aggregate([
  {$unwind: "$TRAFFIC-POLICE"},
  {
    $group: {
      _id: "$TRAFFIC-POLICE.tpIdNum",
      Total: {
        $addToSet: {
          Name: "$TRAFFIC-POLICE.tpName",
          ID: "$TRAFFIC-POLICE.tpIdNum"
        }
      },
      "Total Reports": {
        $first: {$size: "$TRAFFIC-POLICE.makeReport"}
      }
    }
  },
  {$unset: "_id"}
])

See how it works on the playground example - separate documents

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文