mongodb嵌套分组和单独的数组
我在下面有此示例BSON文件,我正在寻找每个交通警察的报告总数,对于每个交通警察,我想列出tpidnum
,tpname
和 报告总数makereport
。 我正在尝试实现这样的目标:
{ "_id" : { "Name" : "Kevin Brown", ID: "KBB001"}, "Total Report" : 2},
{ "_id" : { "Name" : "Thomas Moore", ID: "TM001"}, "Total Report" : 1}
这是我到目前为止所得到的,但它一直将tpidnum
和tpname
这样。
{
"_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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于
流量 - Police
是一个数组,因此您需要$ MAP
才能格式化每个项目。$ group
如果每个警察是一个单独的文档,将有所帮助。因此,如果您希望它们在一个文档中(如下):
查看如何在
或,如果要单独的文档,请在
$ $ group
之前使用$ undind
:请参阅游乐场示例 - 单独的文档
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:
See how it works on the playground example - one document
Or, if you want separate documents, use
$unwind
before the$group
:See how it works on the playground example - separate documents