[英]MongoDB Aggregation $match for double nested Object
I try to query my events by their dates.我尝试按日期查询我的事件。 So every event has a start date and a end date.
所以每个事件都有一个开始日期和一个结束日期。
The structure of my Collection where these Events are inside is the following:这些事件所在的集合的结构如下:
{
"_id": "5f2a7feae0c4e10017308fe5",
"events": [
{
"_id": "5f2a802ce0c4e10017308fe8",
"end": {
"dateTime": "2020-08-05T23:46:00.000Z"
},
"start": {
"dateTime": "2020-08-05T22:46:00.000Z"
}
}, ...
]
}
So I tried the following Aggregation Method:所以我尝试了以下聚合方法:
The start and end values are ISO-Strings which are in my case the beginning and the end of an year.开始和结束值是 ISO 字符串,在我的例子中是一年的开始和结束。
Collection.aggregate([
{ $match: {owner: owner_id}},
{$unwind: '$events'},
{ $match :
{ 'events.start.dateTime':
{$gte: start,
$lt: end
}
}
},
{$sort: {'events.start.dateTime': 1}},
{$group: {_id: '$_id', 'events': {$push: '$events'}}}
])
When I try it with the $match filter I get an empty Array back.当我尝试使用$match过滤器时,我得到一个空数组。 But if I try it without the $match filter.
但是,如果我在没有$match过滤器的情况下尝试它。 I get all events from the collection back.
我从集合中获取所有事件。
Does someone know where the problem could be?有人知道问题可能出在哪里吗?
Actually the new Date('2020-06-17T10:03:46.000Z') did the trick for me.实际上 new Date('2020-06-17T10:03:46.000Z') 对我有用。
So my query looks like the following:所以我的查询如下所示:
Collection.aggregate([
{ $match: { $and: [ {owner: owner_id}, { 'events.start.dateTime': {"$gte": new Date(start), "$lt": new Date(end) } } ] } },
{$unwind: '$events'},
{$sort: {'events.start.dateTime': 1}},
{$group: {_id: '$_id', 'events': {$push: '$events'}}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.