Mongo:匹配聚合查询中的日期似乎被忽略了 [英] Mongo: dates in match aggregate query seem to be ignored

查看:51
本文介绍了Mongo:匹配聚合查询中的日期似乎被忽略了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在mongo数据库中运行聚合语句.我有一个文档,其结构(至少)如下:

I'm trying to run an aggregation statement in my mongo db. I have a document whose structure is (at least) as follows:

{
   "_id": ObjectId,
   "date": ISODate,
   "keywordGroupId": NumberLong,
   "ranking": NumberLong,
}

我想运行一个聚合语句,以聚合给定'keywordGroupId'和给定'日期'间隔的'排名'字段.

I would like to run an aggregation statement that aggregates the 'ranking' field for a given 'keywordGroupId' and a given 'date' interval.

我一直在尝试使用以下聚合命令:

I have been trying with the following aggregate command:

{ 
    aggregate : "KeywordHistory", 
    pipeline : [
        { $match: { keywordGroupId: 75 , "$date": {$gte: ISODate("2013-01-01T00:00:00.0Z"), $lt: ISODate("2013-02-01T00:00:00.0Z")}} },
        { $group: { _id: { null }, count: { $sum: "$ranking" } } }
    ]
}

此命令执行无错误,并返回结果.如果我尝试更改'keywordGroupId'字段的值,该命令将返回一个不同的值,因此我假定$ match语句适用于该字段(NumberLong).但是,如果更改了日期"范围,并且指定了数据库中没有任何数据的时间间隔,它仍然会返回结果(我实际上希望结果为空).因此,我必须假定$ match语句忽略了指定的日期间隔.

This command executes without errors and returns a result. If I try to change the value for the 'keywordGroupId' field, the command returns a different value, so I assume that the $match statement works for that field (NumberLong). Though, if I change the 'date' range and I specify a time interval for which I don't have any data in the database, it still returns a result (I would actually expect an empty result set). So I have to assume that the $match statement is ignoring the date interval specified.

有人可以帮我吗?

推荐答案

删除$match$date字段上的$前缀:

{ $match: { 
    keywordGroupId: 75, 
    date: {$gte: ISODate("2013-01-01T00:00:00.0Z"), $lt: ISODate("2013-02-01T00:00:00.0Z")}
}},

仅当将字段名称用作值而不是键时,才使用$前缀.

You only use the $ prefix when the field name is used in a value, not as a key.

这篇关于Mongo:匹配聚合查询中的日期似乎被忽略了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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