mongodb Mongo:匹配聚合查询中的日期似乎被忽略

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15024383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:03:41  来源:igfitidea点击:

Mongo: dates in match aggregate query seem to be ignored

mongodbaggregation-framework

提问by Marco

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

我正在尝试在我的 mongo db 中运行聚合语句。我有一个文件,其结构(至少)如下:

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

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

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

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" } } }
    ]
}

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.

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

Can anyone help me with this point?

谁能帮我解决这个问题?

回答by JohnnyHK

Remove the $prefix on the $datefield of your $match:

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

{ $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.

$当字段名称用于值中时才使用前缀,而不是用作键。

回答by Ravi Joshi

Sometimes ISodate does not works . so in Case if you want to match date using only "one" date the best way is:---

有时 ISOdate 不起作用。因此,如果您只想使用“一个”日期匹配日期,最好的方法是:---

ex:-- Let a schema be:---

例如:-- 让架构为:-

var storeOrder = new Schema({
store_name:{type:String, required:true},
date :{type:Date ,default:moment(new Date()).format('YYYY-MM-DD')},
orders : [{
vegetable : String,
quantity : Number,
price:Number
 }]

});

});

mongoose.model('storeorder',storeOrder);

now to aggregate by matching date :--

现在按匹配日期聚合:--

storeOrder.aggregate([$match:{date :new Date("2016-12-26T00:00:00.000Z")} ])

**It is must to use new Date("2016-12-26T00:00:00.000z")instead of Date("2016-12-26T00:00:00.000z") because Date(your_date) !== new Date(your_date).

**必须使用new Date("2016-12-26T00:00:00.000z")而不是Date("2016-12-26T00:00:00.000z") because Date(your_date) !== new Date(your_date).

THANK YOU

谢谢你