首页 > 解决方案 > 如何根据结果内部对象对mongo聚合查询进行排序

问题描述

如果 mongo 聚合结果是:

{
  _id : "5c1a39d182ceb728c3430275",
  docs : {
    "First Name": "Nilesh",
    "Last Name" : "Sodha",
    "Age"       : 23    
  }
},

{
  _id : "5c1a39d182ceb728c3430274",
  docs : {
    "First Name": "Pradip",
    "Last Name" : "Sodha",
    "Age"       : 25    
  }
}

如何在JAVA中获得基于“年龄”的排序结果

标签: javaspringmongodbspring-bootspring-mvc

解决方案


尝试这个

 mongoTemplate.find(new Query(Criteria.where("_id").is(mongoId))
                    .with(new Sort(Sort.Direction.DESC, "Age")), eClass);

mongoId = 自动生成的 mongo id


推荐阅读