【发布时间】:2021-06-01 12:01:52
【问题描述】:
我仍在与 MongoDb 一起学习 Java Spring,现在遇到了一个我找不到任何解决方案的问题。
我在 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();
当我列出 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 专家可以立即看到我做错了什么,如果你能帮助我,我真的很感激!谢谢。
【问题讨论】:
标签: java spring mongodb sorting aggregation