本文介绍了基于 Map Key Spring Repository 查询 MongoDb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我需要帮助来查询嵌套文档.将 Spring Boot 与 MongoDB 结合使用.结构:公共课假期{@ID私人字符串ID;私人整数年;私有地图>假期 = 新的 HashMap();}公共类假日元素{私人字符串名称;@JsonFormat(pattern="yyyy-MM-dd")私人日期;私人字符串笔记;}保存 Json 的所有内容后:[{"id": "5a153331b3cb1f0001e1edeb",年份":2017,假期":{BB":[{"name": "Neujahrstag","日期": "2017-01-01",笔记": ""},...],呵呵":[{ ... }]}]现在我如何获得例如:状态为BB"的HolidayElement"列表? 解决方案 假设您有一个像 HolidayRepository 这样的存储库,您需要创建一个自定义实现,因为您想使用 MongoTemplate.所以你的 HolidayRepository 看起来像 @Repository公共接口 HolidayRepository 扩展 MongoRepository, HolidayRepositoryCustom {}并声明两个新文件HolidayRepositoryCustom 和HolidayRepositoryImpl 与HolidayRepository 位于同一目录下(非常重要)公共接口 HolidayRepositoryCustom {列表findByMapId(final String mapId);}Impl 类看起来像这样public class HolidayRepositoryImpl 实现 HolidayRepositoryCustom {私人最终 MongoTemplate mongoTemplate;@自动连线public HolidayRepositoryImpl(final MongoTemplate mongoTemplate) {this.mongoTemplate = mongoTemplate;}@覆盖公共列表findByMapId(String mapId) {最终 QueryBuilder queryBuilder = QueryBuilder.start();查询生成器.and("holidays."+mapId).exists(true);最终的 DBObject 投影 = new BasicDBObject();投影.put("假期."+mapId, 1);String collectionName = "Holiday";//改成你的收藏名尝试(最终 DBCursor dbCursor = mongoTemplate.getCollection(collectionName).find(queryBuilder.get(),projection)){如果(dbCursor.hasNext()){DBObject next = dbCursor.next();地图>假期元素 =(Map>) next.get("holidays");返回holidayElements.get(mapId);}}返回 Lists.newArrayList();}}I need help to query nested documents. Using Spring Boot with MongoDB.Structure:public class Holiday { @Id private String id; private Integer year; private Map<String, List<HolidayElement>> holidays = new HashMap<>();}public class HolidayElement { private String name; @JsonFormat(pattern="yyyy-MM-dd") private Date date; private String note;}After saving everything the Json looks like:[ { "id": "5a153331b3cb1f0001e1edeb", "year": 2017, "holidays": { "BB": [ { "name": "Neujahrstag", "date": "2017-01-01", "note": "" }, ... ], "HH": [ { ... } ] } ]Now how can I get for instance: List of "HolidayElement" where the State is "BB"? 解决方案 Assuming you have a repository like HolidayRepository, you need to create a custom implementation since you want to use MongoTemplate. So your HolidayRepository will look like @Repositorypublic interface HolidayRepository extends MongoRepository<Holiday, String>, HolidayRepositoryCustom {} And declare two new files HolidayRepositoryCustom and HolidayRepositoryImpl in the same directory(very important) as HolidayRepositorypublic interface HolidayRepositoryCustom { List<HolidayElement> findByMapId(final String mapId);}And the Impl class will look like this public class HolidayRepositoryImpl implements HolidayRepositoryCustom { private final MongoTemplate mongoTemplate; @Autowired public HolidayRepositoryImpl(final MongoTemplate mongoTemplate) { this.mongoTemplate = mongoTemplate; } @Override public List<HolidayElement> findByMapId(String mapId) { final QueryBuilder queryBuilder = QueryBuilder.start(); queryBuilder .and("holidays."+mapId).exists(true); final DBObject projection = new BasicDBObject(); projection.put("holidays."+mapId, 1); String collectionName = "Holiday";//Change to your collection name try( final DBCursor dbCursor = mongoTemplate.getCollection(collectionName).find(queryBuilder.get(), projection)){ if(dbCursor.hasNext()){ DBObject next = dbCursor.next(); Map<String, List<HolidayElement>> holidayElements = (Map<String, List<HolidayElement>>) next.get("holidays"); return holidayElements.get(mapId); } } return Lists.newArrayList(); }} 这篇关于基于 Map Key Spring Repository 查询 MongoDb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-20 10:08