1
我一直在尝试根据日期对存储在列表中的项目进行排序。这里是我的文档的结构:Spring数据Mongodb排序内部数组
{
"_id": ObjectId("55c1940ce4b0a2a0482f22e8"),
"eventId": "45b73f69-4804-4b50-84d0-6c551aa5bb5c",
"isMacro": true,
"isDeleted": false,
"calendarEvent": [
{
"eventId": "b89a1519-ddff-4f71-9366-117e23c16d5c",
"startDateTime": ISODate("2017-04-16T10:00:00.000Z")
},
{
"eventId": "ecec8b3a-cf48-4f14-a7ac-14cd1a19f9da",
"startDateTime": ISODate("2014-03-25T10:00:00.000Z")
},
{
"eventId": "a002cc47-2939-4e75-9793-74720ebb7a21",
"startDateTime": ISODate("2015-09-10T10:00:00.000Z")
}
]
}
我想在ASC
顺序calendarEvent
列表中的项目进行排序。我正在使用Spring Data MongoDB。 这里是我的代码:
AggregationOperation unwind = Aggregation.unwind("calendarEvent");
AggregationOperation match = Aggregation.match(criteria);
AggregationOperation project = getEventProjectionFields(); //private function which returns project object.
AggregationOperation sort = Aggregation.sort(Direction.ASC,
"calendarEvent.startDateTime");
Aggregation aggregation = Aggregation.newAggregation(unwind, match,
project, skip(0), limit(5), sort);
AggregationResults<Event> groupResults = mongoTemplate.aggregate(
aggregation, mongoTemplate.getCollectionName(KAEvent.class),
Event.class);
return groupResults.getMappedResults();
印刷aggregation.toString()
返回以下JSON值:
{
"aggregate": "__collection__",
"pipeline": [
{
"$unwind": "$calendarEvent"
},
{
"$match": {
"isMacro": true,
"isDeleted": false,
"$and": [
{
"eventId": {
"$in": [
"3d478f1a-9296-46b5-87d9-d0b442be0309",
"4cbbe84b-6a35-4797-8c06-9b9b679ca48c"
]
}
},
{
"calendarEvent.eventId": {
"$in": [
"cb3fb7e7-5444-4ca5-8a6b-09e74bd346e4",
"fec94eb6-160d-4608-8dae-5b43b88c550f",
"e33d11a1-6d36-49ce-9e84-9b83ee9445dd"
]
}
}
],
"calendarEvent.startDateTime": {
"$gte": {
"$date": "2015-10-13T07:42:48.841Z"
}
}
}
},
{
"$project": {
"eventId": "$calendarEvent.eventId",
"name": "$calendarEvent.name",
"eventDescription": "$calendarEvent.eventDescription",
"eventSummary": "$calendarEvent.eventSummary",
"eventLocation": "$calendarEvent.eventLocation",
"startDateTime": "$calendarEvent.startDateTime",
"endDateTime": "$calendarEvent.endDateTime",
"lastUpdateDate": "$calendarEvent.lastUpdateDate",
"eventUrl": "$calendarEvent.eventUrl",
"customImageUrl": "$calendarEvent.customImageUrl",
"tagsList": "$calendarEvent.tagsList",
"imagesList": "$calendarEvent.imagesList",
"isFeatured": "$calendarEvent.isFeatured",
"isDeleted": "$calendarEvent.isDeleted",
"providerId": 1,
"sourceId": 1,
"isMacro": 1,
"isCalendarEvent": 1,
"_class": "$calendarEvent._class",
"parenteventId": "$eventId"
}
},
{
"$skip": 0
},
{
"$limit": 5
},
{
"$sort": {
"calendarEvent.startDateTime": 1
}
}
]
}
这一切后,返回的结果进行排序。我在这里错过了什么或做错了什么?
尝试这样很好,但仍然得到不正确的排序顺序。 –
您是否还尝试在文档[** here **]中建议的限制管道之前进行排序(http://docs.mongodb.org/manual/core/aggregation-pipeline-optimization/#sort-limit合并)和[**存在**](http://docs.mongodb.org/manual/reference/operator/aggregation/sort/#sort-limit-memory-optimization)以获得更好的性能? – chridam
@SibtainNorain:我有同样的要求,仍然无法使其工作。您能否确认Chridam为您提供的解决方案,并且您未对上述原始代码进行其他更改? – shkhssn