Search code examples
javaspringmongodbsortingaggregation

The result is not sorted using aggregation and MongoDB


I'm still learning Java Spring together with MongoDb and had now an issue that I cannot find any solution.

I have the following data in MongoDB:

{
    "_id" : ObjectId("603ea2eddb5621d54362d2a9"),
    "albumKey" : "3ee258ca5aef4e37a8b0bc0ff7cc3256",
    "albumName" : "Profile photos",
    "createdOn" : ISODate("2021-03-02T20:41:17.626Z"),
    "deleted" : false,
    "createdBy" : NumberLong(1),
    "privacy" : "public",
    "_class" : "com.f2.AuthApp.model.photo.PhotoAlbums",
    "albumPhotos" : [ 
        {
            "photoKey" : "25a370e24aa74cb49bdac85be311cc14",
            "fileName" : "3kZ3hc2YMB6LXiPohtyfKa(8).jpg",
            "createdOn" : ISODate("2021-03-02T20:41:17.640Z"),
            "deleted" : false,
            "userId" : NumberLong(1),
            "privacy" : "public"
        }, 
        {
            "photoKey" : "d6a2865356974a8d9c4c56ff780eb86f",
            "fileName" : "aliastudioportraitwebsize2048ver2(2).jpg",
            "createdOn" : ISODate("2021-03-02T20:41:26.099Z"),
            "deleted" : false,
            "userId" : NumberLong(1),
            "privacy" : "public"
        }, 
        {
            "photoKey" : "7a32b8ac45f542519a3615785c15a6cc",
            "fileName" : "DigitalGlobeWorldView150cm8bitBWDRABangkokThailand2009JAN068bitssubr1(2).jpg",
            "createdOn" : ISODate("2021-03-02T20:41:41.761Z"),
            "deleted" : false,
            "userId" : NumberLong(1),
            "privacy" : "public"
        }, 
        {
            "photoKey" : "2a289bb9b4a24a1db1a2153345802389",
            "fileName" : "fc03426a5fac006d576da53970a21403(1).jpg",
            "createdOn" : ISODate("2021-03-02T20:42:26.717Z"),
            "deleted" : false,
            "userId" : NumberLong(1),
            "privacy" : "public"
        }
    ]
}

and I'm trying extract what is in albumPhotos by using:

        AggregationOperation unwind = Aggregation.unwind("albumPhotos");
        AggregationOperation match = Aggregation.match(Criteria.where("createdBy").is(customUserDetails.getId()));
        AggregationOperation match2 = Aggregation.match(Criteria.where("albumPhotos.deleted").is(false));
        AggregationOperation sort = Aggregation.sort(Sort.Direction.ASC, "albumPhotos.createdOn");
        AggregationOperation group = Aggregation.group("albumPhotos");

        Aggregation aggregation = Aggregation.newAggregation(match, unwind, match2, sort, group);

        AggregationResults<AlbumPhotos> albumPhotos = mongoTemplate.aggregate(aggregation, PhotoAlbums.class, AlbumPhotos.class);


        List<AlbumPhotos> albumPhotosList = albumPhotos.getMappedResults();

and when I'm listed List the result is not in sorted by createdOn as expected, it is something like:

[
    {
        "photoKey": "25a370e24aa74cb49bdac85be311cc14",
        "fileName": "3kZ3hc2YMB6LXiPohtyfKa(8).jpg",
        "createdOn": 1614717677640,
        "deleted": false,
        "deletedOn": null,
        "userId": 1,
        "privacy": "public"
    },
    {
        "photoKey": "2a289bb9b4a24a1db1a2153345802389",
        "fileName": "fc03426a5fac006d576da53970a21403(1).jpg",
        "createdOn": 1614717746717,
        "deleted": false,
        "deletedOn": null,
        "userId": 1,
        "privacy": "public"
    },
    {
        "photoKey": "7a32b8ac45f542519a3615785c15a6cc",
        "fileName": "DigitalGlobeWorldView150cm8bitBWDRABangkokThailand2009JAN068bitssubr1(2).jpg",
        "createdOn": 1614717701761,
        "deleted": false,
        "deletedOn": null,
        "userId": 1,
        "privacy": "public"
    },
    {
        "photoKey": "d6a2865356974a8d9c4c56ff780eb86f",
        "fileName": "aliastudioportraitwebsize2048ver2(2).jpg",
        "createdOn": 1614717686099,
        "deleted": false,
        "deletedOn": null,
        "userId": 1,
        "privacy": "public"
    }
]

I'm looking for an answer since 2 days ago what I'm doing wrong sorting this inner array and without success.

Perhaps the experts in Java can see immediately what I'm doing wrong and I really appreciate if you can help me! Thank you.


Solution

  • You have a group stage after the sort stage. Unless you can find a statement in documentation for $group stating the output is sorted, which I don't see, you need to sort after grouping.