获取最大值并计入相同的查询mongodb

时间:2017-05-26 19:14:44

标签: mongodb aggregation-framework

我正在尝试在执行导出之前验证数据。在将数据添加到集合之前,大多数人会先验证数据,但在这种情况下,由于有人需要在人员注册后分配网络和团队,因此无法完成。所以,我试图做的是检查序列中缺少的网络#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"}}}
        ]);

有人可以帮我理解如何解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

如果我理解你的问题,这会给你你想要的东西:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/toolbar" android:id="@+id/toolbar"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true"
        android:layout_marginTop="20dp"
        android:id="@+id/articlesummaryimageview"
        android:layout_below="@id/toolbar"
        android:layout_centerHorizontal="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/articlesummaryheadingtextview"
        android:layout_below="@id/articlesummaryimageview"
        android:layout_centerHorizontal="true"
        android:textSize="26dp"
        android:textStyle="bold"
        android:textAlignment="center"
        android:layout_marginTop="20dp" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/articlesummaryscreenscrollview"
        android:layout_below="@id/articlesummaryheadingtextview"
        android:fillViewport="true"
        android:layout_above="@+id/showfullarticlebutton">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/articlesummarytextview"
                android:textSize="18dp"
                android:textAlignment="center"
                android:padding="5dp"
                android:text="this is a text"/>

        </RelativeLayout>
    </ScrollView>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/showfullarticlebutton"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:textSize="18dp"
        android:text="Full Article"
        style="@style/AlertDialog.AppCompat.Light" />
</RelativeLayout>

答案 1 :(得分:1)

好的,在上面的精彩贡献者的帮助下,我能够使用它:

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"}}}仅显示那些数字不相同的事件(我们跳过一个网络号,或者在两个不同的字段中使用相同的一个)。不知道我做了什么让这部分不起作用,但它以最干净的方式显示了我想要的结果。