在执行导出之前,我正在尝试验证数据。大多数人会首先验证数据,然后再将其添加到集合中,但在这种情况下,无法进行验证,因为需要在人员注册后分配网络和团队。所以,我要做的是检查序列中缺少的net#s。它应该去1,2,3,等等。我想我可以按事件分组,并使用$max来获得为该事件分配的最高的网。然后,我可以比较唯一的网数和$max数。如果他们是平等的,我们是好的。我想不出如何正确地进行查询。$max总是空的,或者没有出现。
样本数据:
{ "_id" : ObjectId("591c7e1710c42a02a1833fef"), "event" : "Junior Girls 16s", "field" : "Main", "day" : "Friday", "division" : "Junior Girls", "level" : "16s", "group" : "nonpro", "numplayers" : 2, "price" : 50, "net" : 1, "team" : 1 },
{ "_id" : ObjectId("591c7e1710c42a02a1833fef"), "event" : "Junior Girls 16s", "field" : "Main", "day" : "Friday", "division" : "Junior Girls", "level" : "16s", "group" : "nonpro", "numplayers" : 2, "price" : 50, "net" : 1, "team" : 2 },
{ "_id" : ObjectId("591c7e1710c42a02a1833fef"), "event" : "Junior Girls 16s", "field" : "Main", "day" : "Friday", "division" : "Junior Girls", "level" : "16s", "group" : "nonpro", "numplayers" : 2, "price" : 50, "net" : 1, "team" : 3 },
{ "_id" : ObjectId("591c7e1710c42a02a1833fef"), "event" : "Junior Girls 16s", "field" : "Main", "day" : "Friday", "division" : "Junior Girls", "level" : "16s", "group" : "nonpro", "numplayers" : 2, "price" : 50, "net" : 1, "team" : 4 }
{ "_id" : ObjectId("591e07186f335b497bef0f9a"), "event" : "Junior Girls 16s", "field" : "Main", "day" : "Friday", "division" : "Junior Girls", "level" : "16s", "group" : "nonpro", "numplayers" : 2, "price" : 50, "net" : 3, "team" : 1 },
{ "_id" : ObjectId("591e07186f335b497bef0f9a"), "event" : "Junior Girls 16s", "field" : "Main", "day" : "Friday", "division" : "Junior Girls", "level" : "16s", "group" : "nonpro", "numplayers" : 2, "price" : 50, "net" : 3, "team" : 2 },
{ "_id" : ObjectId("591e07186f335b497bef0f9a"), "event" : "Junior Girls 16s", "field" : "Main", "day" : "Friday", "division" : "Junior Girls", "level" : "16s", "group" : "nonpro", "numplayers" : 2, "price" : 50, "net" : 3, "team" : 3 }我希望这些数据能够显示出来:
event maxNet count (count of unique event/net field combo)
Junior Girls 16s 3 2我尝试过的许多事情之一:
db.registrations.aggregate([
{$match: {event: "Juniors Girls 16s"}},
{$group: {_id: {event: "$event", net: "$net"}, count: {$sum:1}}},
{ $project: {event: "$event", count: {$sum: 1}, maxNet: {$max: "$_id.net"}}}
]);有人能帮我弄清楚我怎么能修好它吗?
发布于 2017-05-30 22:42:54
好吧,在上面你们这些出色的贡献者的帮助下,我能够让它发挥作用:
db.registrations.aggregate([
{ "$match": searchParams },
{ "$group": { "_id": { "event": "$event" },
"netCount": {$addToSet: "$net"},
"maxNet":{$max: "$net"} }},
{$project: {nets: {$size: "$netCount"},maxNet: "$maxNet"}},
{$match: {nets: {$ne: "$maxNet"} }}
])这给出了事件,使用的唯一净编号的数目,以及用于该事件的最高净数。我试图使用最后一行:{$match: {nets: {$ne: "$maxNet"}}}只显示那些数字不相同的事件(我们跳过了一个网号,或者在两个不同的字段上使用了相同的一个)。不知道我做了什么使这个部分不工作,但它显示了我想要的结果,以最干净的方式。
发布于 2017-05-26 19:36:55
如果我正确理解你的问题,这将给你你想要的:
db.registrations.aggregate([
{$match: {event: 'Junior Girls 16s'}},
{$group: {_id: {event: "$event", net: "$net"}, count: {$sum:1}}},
{$project: {event: "$_id.event", count: "$count", maxNet: {$max: "$_id.net"}}}
]);https://stackoverflow.com/questions/44208719
复制相似问题