结果未使用聚合和 MongoDB 排序

The result is not sorted using aggregation and MongoDB

我仍在学习 Java Spring 和 MongoDb,现在遇到一个问题,我找不到任何解决方案。

我在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"
        }
    ]
}

我正在尝试使用以下方法提取 albumPhotos 中的内容:

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

当我列出 List 时,结果没有按预期按 createdOn 排序,它类似于:

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

自 2 天前以来,我一直在寻找答案,我在对这个内部数组进行排序时做错了,但没有成功。

也许 Java 中的专家可以立即看出我做错了什么,如果您能帮助我,我将不胜感激!谢谢。

排序阶段之后是小组赛阶段。除非您可以在 $group 的文档中找到说明输出已排序的声明(我没有看到),否则您需要在分组后进行排序。