我必须编写一个按日期聚合和分组的查询。 示例文档就像这样
{
"_id" : ObjectId("567c02f0845d0abb468d99b4"),
"carId" : "345678",
"deviceId" : "073213",
"userId" : ObjectId("567a5e962c960d366fe7c989"),
"trips" : [
{
"startTime" : ISODate("2015-12-24T14:36:32.865Z"),
"_id" : ObjectId("567c02f03223321f2b5581b8"),
"tripON" : false,
"source" : {
"latitude" : 13.07,
"longitude" : 77.6181319
},
"travelTime" : 0,
"destination" : {
"latitude" : 14.07,
"longitude" : 78.6181319
},
"endTime" : ISODate("2015-12-24T14:36:57.416Z"),
"topSpeed" : 11,
"distance" : 231,
"distanceUnit" : "km"
},
{
"startTime" : ISODate("2015-12-24T14:37:08.466Z"),
"_id" : ObjectId("567c03143223321f2b5581ba"),
"tripON" : false,
"source" : {
"latitude" : 13.07,
"longitude" : 77.6181319
},
"travelTime" : 0,
"destination" : {
"latitude" : 14.07,
"longitude" : 78.6181319
},
"endTime" : ISODate("2015-12-24T14:37:54.471Z"),
"topSpeed" : 11,
"distance" : 231,
"distanceUnit" : "km"
},
{
"startTime" : ISODate("2015-12-24T19:16:34.771Z"),
"_id" : ObjectId("567c44928242c35c4b169899"),
"tripON" : false,
"source" : {
"latitude" : 13.07,
"longitude" : 77.6181319
},
"travelTime" : 0,
"destination" : {
"latitude" : 14.07,
"longitude" : 78.6181319
},
"endTime" : ISODate("2015-12-24T19:16:47.616Z"),
"topSpeed" : 11,
"distance" : 231,
"distanceUnit" : "km"
}
]
}
现在同一个用户可以有多辆车。并且查询在逻辑上评估为。查找特定用户的所有行程,无论是什么车,按日期隔离。 所以它就像
25-12-2015
Trip 1
Trip 2
.
.
Trip n
24-12-2015
Trip 1
Trip 2
.
.
Trip n
其中旅行n表示旅行的来源,目的地,旅行时间和距离。
我的尝试。
var result = Trip.aggregate([{
"$unwind": "$trips"
}, {
"$match": {
"userId" : userId,
"trips.startTime": {
"$gte": moment(startDate).toDate(),
"$lte": moment(endDate).toDate()
}
}
}, {
"$group": {
"_id": {
"date": {
"$dayOfMonth": "$trips.startTime"
}
}
}
}]);
这会产生未定义的。我究竟做错了什么 ?怎么纠正呢?