java—结果不使用聚合和mongodb进行排序

lp0sw83n  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(365)

我还在和mongodb一起学习javaspring,现在有一个问题我找不到任何解决方案。
我在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"
        }
    ]
}

我正在尝试使用以下方法提取相册照片中的内容:

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

当我在列表中列出结果时,结果并不像预期的那样按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的Maven们能马上看出我做错了什么,如果你能帮我,我真的很感激!谢谢您。

yzuktlbb

yzuktlbb1#

在排序阶段之后有一个分组阶段。除非您能在$group的文档中找到一条语句,说明输出已排序(我看不到),否则您需要在分组之后进行排序。

相关问题