在mongo中汇总集合以确定计数 [英] Aggregating a collection in mongo to determine counts

查看:75
本文介绍了在mongo中汇总集合以确定计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MongoDB中有一个集合,其中包含几个字段,并且在用户每次执行操作时由我的应用程序服务器写入该集合.一种日志收集.每个文档都类似于以下模式,为简洁起见,省略了一些无关的字段:

I have a collection in MongoDB that contains several fields, and is written to by my application server every time a user performs an action. A sort of log-collection. Each document resembles the following pattern, omitting a few irrelevant fields for the sake of brevity:

{
        "_id" : ObjectId("5536727686e09bab576f1c94"),
        "_user" : ObjectId("5536597b60b2321aadeb8a7c"),
        "_userAgent" : "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0",
        "_systemTime" : ISODate("2015-04-21T15:53:26.272Z")
        ...
}

本周3月21日星期二和3月22日星期三,我进行了一些压力测试.我想构成一个查询,以确定每天在该集合中有多少 distinct 个用户条目.如何计算3月21日(星期二)有多少个不同用户至少有一个条目,以及3月22日(星期三)有多少个不同用户至少有一个条目,这是一个好方法?将日期保留在祖鲁语中就足够了.

I ran some stress tests this week on Tuesday, March 21st, and Wednesday, March 22nd. I would like to form a query that can determine how many distinct users have entries in this collection for either day. What would be a good way of getting a count of how many distinct users have at least one entry on Tuesday, March 21st, and how many distinct users have at least one entry on Wednesday, March 22nd? Keeping the dates in zulu is sufficient.

推荐答案

我认为您不需要此处的汇总.您可以通过 distinct 查询以及长度.

I think you do not need aggregate here. You can achieve what you want with a distinct query together with length.

db.coll.distinct('_user', {
    _systemTime: {
        $gte: ISODate(some time),
        $lt: ISODate(some other time)
    }
}).length

查看更多不同的用例,并不是很多人都知道这种方法.

Check out more use cases of distinct, not a lot of people know about this method.

这篇关于在mongo中汇总集合以确定计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆