3

I have been trying to sort the items stored in a list within a document based on date. Here is the structure of my document:

{
  "_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")
    }
  ]
}

I want to sort the items in calendarEvent list in ASC order. I'm using the Spring Data MongoDB. Here is my code:

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();

Printing the value of aggregation.toString() returns the following 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
      }
    }
  ]
}

After all this, the results return are not sorted. Am I missing something here or doing something wrong?

1 Answer 1

2

You are doing a sort after you project the new field startDateTime which is derived from the embedded field "calendarEvent.startDateTime".

Change the sort pipeline to

AggregationOperation sort = Aggregation.sort(Direction.ASC,
        "startDateTime");
Sign up to request clarification or add additional context in comments.

3 Comments

Tried this as well but still getting incorrect sorting order.
Have you also tried placing the sort just before the limit pipeline as suggested in the docs here and there for better performance?
@SibtainNorain : I'm having the same requirement and still not able to make it work. Could you please confirm this solution suggested by Chridam work for you and no other changes you made to your original code above?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.