我的数据库中有类似的数据
{
_id: ObjectId(<some mongo default encoded id>),
user: 45,
event_group: 1,
events: [
{event: "event name", current_points:1, date: ISO(2017-05-11 21:00:45.774Z)},
{event: "event name1", current_points:2, date: ISO(2017-05-11 22:15:45.774Z)},
{event: "event name2", current_points:5, date: ISO(2017-05-11 22:10:55.774Z)},
{event: "event name3", current_points:8, date: ISO(2017-05-12 23:00:45.774Z)},
{event: "event name4", current_points:10, date: ISO(2017-05-12 21:00:45.774Z)},
{event: "event name5", current_points:25, date: ISO(2017-05-14 22:15:45.774Z)},
{event: "event name6", current_points:20, date: ISO(2017-05-14 22:10:55.774Z)},
]
}
{
_id: ObjectId(<some mongo default encoded id>),
user: 45,
event_group: 2,
events: [
{event: "event name", current_points:10, date: ISO(2017-05-11 21:00:45.774Z)},
{event: "event name1", current_points:15, date: ISO(2017-05-11 22:15:45.774Z)},
{event: "event name2", current_points:12, date: ISO(2017-05-11 22:10:55.774Z)},
{event: "event name3", current_points:18, date: ISO(2017-05-12 23:00:45.774Z)},
{event: "event name4", current_points:25, date: ISO(2017-05-12 21:00:45.774Z)},
{event: "event name5", current_points:50, date: ISO(2017-05-14 22:15:45.774Z)},
{event: "event name6", current_points:100, date: ISO(2017-05-14 22:10:55.774Z)},
]
}
{
_id: ObjectId(<some mongo default encoded id>),
user: 44,
event_group: 1,
events: [
{event: "event name", current_points:20, date: ISO(2017-05-11 21:00:45.774Z)},
{event: "event name1", current_points:15, date: ISO(2017-05-11 22:15:45.774Z)},
{event: "event name2", current_points:30, date: ISO(2017-05-11 22:10:55.774Z)},
{event: "event name3", current_points:35, date: ISO(2017-05-12 23:00:45.774Z)},
{event: "event name4", current_points:45, date: ISO(2017-05-12 21:00:45.774Z)},
{event: "event name5", current_points:100, date: ISO(2017-05-14 22:15:45.774Z)},
{event: "event name6", current_points:120, date: ISO(2017-05-14 22:10:55.774Z)},
]
}
我想在7天,15天,30天或更长时间内获取记录。我需要获取所有日期(每天)的用户记录。目前我正在使用以下查询获取它们:
db.Collection.aggregate(
{"$match":{"user":181}},{"$unwind":"$events"},{"$match":{"events.date":{"$gte":{"sec":1523471400,"usec":0},"$lte":{"sec":1523557799,"usec":0}}}},{"$sort":{"events.date":1}},{"$group":{"_id":null,,"points":{"$last":"$events.current_points"}}});
它获取1个日期范围的记录,我运行此查询7,15,30或更多次。查询和数据运行良好,但它在我的服务器上添加了更多处理。我想将这个查询添加到一个单一查询中,就像这样。以下是7天内提取记录的查询。
感谢您解决我的查询Vince Bowdren中的错误。这是我更新的查询。
如何通过我从中获取此数据的日期对此结果进行分组。例如,如果日期介于2017-05-23 00:00:00&amp; 2017-05-23 23:59:59然后,结果也应该通过这个日期分组。
我需要获取所有这些数据,以便通过日期在图表中绘制它们。
db.getCollection('Collection').aggregate(
{"$match":{"user":121}},
{"$unwind":"$events"},
{"$match":{"$or":[
{"events.date":{"$gte":{"sec":1522780200,"usec":0},"$lte":{"sec":1522866599,"usec":0}}},
{"events.date":{"$gte":{"sec":1522866600,"usec":0},"$lte":{"sec":1522952999,"usec":0}}},
{"events.date":{"$gte":{"sec":1522953000,"usec":0},"$lte":{"sec":1523039399,"usec":0}}},
{"events.date":{"$gte":{"sec":1523039400,"usec":0},"$lte":{"sec":1523125799,"usec":0}}},
{"events.date":{"$gte":{"sec":1523125800,"usec":0},"$lte":{"sec":1523212199,"usec":0}}},
{"events.date":{"$gte":{"sec":1523212200,"usec":0},"$lte":{"sec":1523298599,"usec":0}}},
{"events.date":{"$gte":{"sec":1523298600,"usec":0},"$lte":{"sec":1523384999,"usec":0}}},
{"events.date":{"$gte":{"sec":1523385000,"usec":0},"$lte":{"sec":1523471399,"usec":0}}},
{"events.date":{"$gte":{"sec":1523471400,"usec":0},"$lte":{"sec":1523557799,"usec":0}}},
{"events.date":{"$gte":{"sec":1523557800,"usec":0},"$lte":{"sec":1523644199,"usec":0}}},
{"events.date":{"$gte":{"sec":1523644200,"usec":0},"$lte":{"sec":1523730599,"usec":0}}},
{"events.date":{"$gte":{"sec":1523730600,"usec":0},"$lte":{"sec":1523816999,"usec":0}}},
{"events.date":{"$gte":{"sec":1523817000,"usec":0},"$lte":{"sec":1523903399,"usec":0}}},
{"events.date":{"$gte":{"sec":1523903400,"usec":0},"$lte":{"sec":1523989799,"usec":0}}},
{"events.date":{"$gte":{"sec":1523989800,"usec":0},"$lte":{"sec":1524076199,"usec":0}}}
]}},
{"$sort":{"events.date":1}},
{"$group":{"_id":"$user","events":{"$push":"$events"},
"points":{"$last":"$events.current_points"},
}
}
)
目前我收到了这个结果:
{
"result": [{
"_id": 121,
"points": 42930,
"events": [{
"current_points": 41875,
"won": 1,
"date": {
"sec": 1522780225,
"usec": 237000
},
"event_name": "abc"
}, {
"current_points": 41875,
"won": 1,
"date": {
"sec": 1522780225,
"usec": 237000
},
"event_name": "absc"
}, {
"current_points": 41875,
"won": 0,
"date": {
"sec": 1522780225,
"usec": 237000
},
"event_name": "abcd"
}]
}]
}
答案 0 :(得分:0)
这不是你应该如何在$match
块内编写标准。编辑您的查询,使其看起来像这样:
{"$or":[
{"sec":{"$gte":1523471400}},{"usec":{"$gte":0}},{"sec":{"$lte":1523557799}},{"usec":{"$lte":0}}
....... //other criteria to be added.
]
}
另请勿重复usec
的条件检查。只需添加剩余的sec
匹配条件条件。
答案 1 :(得分:0)
您错误地使用了$or conditional。
而不是:
{"$match":{"events.date":
{"$or":[
{"$gte":{"sec":1523471400,"usec":0},"$lte":{"sec":1523557799,"usec":0}},
{"$gte":{"sec":1523557800,"usec":0},"$lte":{"sec":1523644199,"usec":0}},
{"$gte":{"sec":1523644200,"usec":0},"$lte":{"sec":1523730599,"usec":0}},
{"$gte":{"sec":1523730600,"usec":0},"$lte":{"sec":1523816999,"usec":0}},
{"$gte":{"sec":1523817000,"usec":0},"$lte":{"sec":1523903399,"usec":0}},
{"$gte":{"sec":1523903400,"usec":0},"$lte":{"sec":1523989799,"usec":0}},
{"$gte":{"sec":1523989800,"usec":0},"$lte":{"sec":1524076199,"usec":0}}
]
}
}},
$或需要一组独立查询,每个查询包括他们查询的字段。尝试将其更改为:
{"$match":{
"$or":[
{"events.date" : {"$gte":{"sec":1523471400,"usec":0},"$lte":{"sec":1523557799,"usec":0}}},
{"events.date" : {"$gte":{"sec":1523557800,"usec":0},"$lte":{"sec":1523644199,"usec":0}}},
{"events.date" : {"$gte":{"sec":1523644200,"usec":0},"$lte":{"sec":1523730599,"usec":0}}},
{"events.date" : {"$gte":{"sec":1523730600,"usec":0},"$lte":{"sec":1523816999,"usec":0}}},
{"events.date" : {"$gte":{"sec":1523817000,"usec":0},"$lte":{"sec":1523903399,"usec":0}}},
{"events.date" : {"$gte":{"sec":1523903400,"usec":0},"$lte":{"sec":1523989799,"usec":0}}},
{"events.date" : {"$gte":{"sec":1523989800,"usec":0},"$lte":{"sec":1524076199,"usec":0}}
]
}
},