使用聚合在mongoDB中限制和排序每个组

8 sorting mongodb mongodb-query aggregation-framework

如何在mongoDB中对每个组进行排序和限制.

考虑以下数据:

Country:USA,name:xyz,rating:10,id:x
Country:USA,name:xyz,rating:10,id:y
Country:USA,name:xyz,rating:10,id:z
Country:USA,name:abc,rating:5,id:x
Country:India,name:xyz,rating:5,id:x
Country:India,name:xyz,rating:5,id:y
Country:India,name:abc,rating:10,id:z
Country:India,name:abc,rating:10,id:x
Run Code Online (Sandbox Code Playgroud)

现在说我将按国家分组并按评级排序,并将每组的数据限制为2.

所以答案是:

Country:USA
name:xyz,rating:10,id:x
name:xyz,rating:10,id:y
Country:India
name:abc,rating:10,id:x
name:abc,rating:10,id:z
Run Code Online (Sandbox Code Playgroud)

我想只使用聚合框架来实现这一点.

我尝试将汇总排序用于评级,但只是查询在处理后没有结果.

Bla*_*ven 10

这里你最好的选择是为每个"Country"运行单独的查询(理想情况是并行)并返回组合结果.查询非常简单,只需在对评级值进行排序后返回前2个值,即使您需要执行多个查询以获得完整结果,也会很快执行.

聚合框架现在甚至在不久的将来都不适合这种情况.问题是没有这样的运营商以任何方式"限制"任何分组的结果.所以为了做到这一点,你基本上需要将$push所有内容都放入一个数组中并从中提取"前n个"值.

当前需要做的操作非常糟糕,核心问题是在大多数实际数据源上,结果可能超过每个文档16MB的BSON限制.

n由于你现在必须如何做到这一点,这也是一个复杂的问题.但只是为了演示2项:

db.collection.aggregate([
    // Sort content by country and rating
    { "$sort": { "Country": 1, "rating": -1 } },

    // Group by country and push all items, keeping first result
    { "$group": {
        "_id": "$Country",
        "results": {
            "$push": {
                "name": "$name", 
                "rating": "$rating",
                "id": "$id"
            }
        },
        "first": { 
            "$first": {
                "name": "$name", 
                "rating": "$rating",
                "id": "$id"
            }
        }
    }},

    // Unwind the array
    { "$unwind": "results" },

    // Remove the seen result from the array
    { "$redact": {
        "$cond": {
            "if": { "$eq": [ "$results.id", "$first.id" ] },
            "then": "$$PRUNE",
            "else": "$$KEEP"
        }
    }},

    // Group to return the second result which is now first on stack
    { "$group": {
        "_id": "$_id",
        "first": { "$first": "$first" },
        "second": { 
            "$first": {
                "name": "$results.name", 
                "rating": "$results.rating",
                "id": "$results.id"
            }
        }
    }},

    // Optionally put these in an array format
    { "$project": {
        "results": { 
            "$map": {
                "input": ["A","B"],
                "as": "el",
                "in": {
                    "$cond": {
                        "if": { "$eq": [ "$$el", "A" ] },
                        "then": "$first",
                        "else": "$second"
                    }
                }
            }
        }
    }}
])
Run Code Online (Sandbox Code Playgroud)

这得到了结果,但它不是一个很好的方法,并且在更高限制的迭代中变得更复杂,甚至n在某些情况下分组可能少于返回结果的情况.

当前的开发系列(3.1.x)在编写时有一个$slice操作符,使其更简单,但仍然具有相同的"大小"缺陷:

db.collection.aggregate([
    // Sort content by country and rating
    { "$sort": { "Country": 1, "rating": -1 } },

    // Group by country and push all items, keeping first result
    { "$group": {
        "_id": "$Country",
        "results": {
            "$push": {
                "name": "$name", 
                "rating": "$rating",
                "id": "$id"
            }
        }
    }},
    { "$project": {
        "results": { "$slice": [ "$results", 2 ] }
    }}
])
Run Code Online (Sandbox Code Playgroud)

但基本上直到聚合框架有某种方式来"限制"由$push类似的"限制"运算符产生的项目数量,那么聚合框架实际上并不是这类问题的最佳解决方案.

像这样的简单查询:

db.collection.find({ "Country": "USA" }).sort({ "rating": -1 }).limit(1)
Run Code Online (Sandbox Code Playgroud)

针对每个不同的国家/地区运行,理想情况下,通过线程的事件循环并行处理,并结合使用结果,立即生成最佳方法.它们只获取所需的内容,这是聚合框架在这种分组中无法处理的大问题.

因此,寻找支持以最佳方式为您所选择的语言执行此"组合查询结果",因为它比将其放在聚合框架上要简单得多,性能要高得多.

  • 截至 2019 年,这仍然有意义吗? (2认同)