0

集計したいUserAgent

db.reports.aggregate(
    {
        $group: {
            _id: '$UserAgent',
            docsPerUserAgent: {
                $sum : 1
            }
        }
    }
)

上記のクエリでは、文字列全体 が考慮されますUserAgent。私がやりたいのは、Android(オプションA)、iPhone / iPad(オプションB)、その他(オプションC)を含むものを数えることです。関数を集約フレームワークに提供するにはどうすればよいですkeyfか(これに似たもの-キーをフェッチする関数を指定してグループ化する方法はありますが、集約フレームワークのコンテキストで)?

4

1 に答える 1

0

At the moment there is nothing that allows you to do somehting like:

db.col.aggregate([
    {$project: {userAgentType: {$extract: /(Android|iPhone/iPad)/}}},
    {$group: on_userAgentType}
]);

Maybe this feature exists in the JIRA already however, I couldn't immediately find it.

Instead on of the better ways to do this would be to split the "interesting" parts of the user agent string out into separate fields so you can group by something like userAgent.agentType which represents either android or ipad or iphone. Not only that but you can also use this to produce a $match before hand which should mean you can use an index on this field making your aggregation a lot more performant in general.

于 2013-03-18T14:17:38.190 に答える