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