[英]using $group and $match (with range of date) functions in pymongo query
im trying to execute the following sql query in Mongodb 我试图在Mongodb中执行以下SQL查询
select organization from users
group by organization
having 2014-07-10 > date > 2014-07-10
(i dnt knw if SQL syntax is correct or not, but i hope i have given a rough i what im trying to do here) (如果SQL语法正确与否,我就不知道,但是我希望我已经大致给出了我要在这里做什么)
first i tried to see how many users were added yesterday 首先,我尝试查看昨天增加了多少用户
>>> users.find({u"timeCreated":{"$gte":date_yesterday, "$lt":date_today}}).count()
6
now i tried to group the users by the organizations to which they belong and used the following monogo aggregate query 现在,我尝试按用户所属的组织对用户进行分组,并使用以下monogo聚合查询
>>> user.aggregate([{'$group':{'_id': "$organization",'count':{"$sum":1}}},{"match":{u'timeCreated':{"$gte":date_yesterday, "$lt":date_today}}}])
result was 结果是
{u'ok': 1.0, u'result':[]}
that is no such query. 那不是这样的查询。
i even tried using the following query to get all entries dated before today 我什至尝试使用以下查询获取今天之前的所有条目
>>> user.aggregate([{'$group':{'_id': "$organization",'count':{"$sum":1}}},{"match":{u'timeCreated':{"$lt":date_today}}}])
but the output remains the same. 但输出保持不变。
the following command works fine and give a list of organizations 以下命令可以正常工作并提供组织列表
>>> user.aggregate({'$group':{'_id': "$organization",'count':{"$sum":1}}})
can any one tell me where im going wrng?? 谁能告诉我我要去哪里?
PS I m new to mongoDB PS我是mongoDB的新手
The other way around. 另一种方式。 You want to match the items first
您要先匹配项目
user.aggregate([
{"$match": {'timeCreated':{ "$gte":date_yesterday, "$lt":date_today } }},
{'$group':{'_id': "$organization",'count':{"$sum": 1 } }}
])
Also note that as this is a "pipeline" operations like $group
or $project
are "destructive" and only emit the fields you actually specify. 还要注意,由于这是“管道”操作,例如
$group
或$project
是“破坏性的”操作,只会发出您实际指定的字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.