Spring + MongoDB - MongoTemplate + Criteria Query

这一生的挚爱 提交于 2021-01-29 05:01:32

问题


I am using Spring Boot + MongoDB. I need to query database based on some criteria where my methods looks like below:

@Override
    public List<MyCollection> findBuyByCriteria(Request request) {
        Query search = new Query();
        search.addCriteria(Criteria.where("ItmId").in(request.getItmIds()));
        return mongoTemplate.find(search, MyCollection.class);
    }

Problem that I am facing is: At line

search.addCriteria(Criteria.where("ItmId").in(request.getItmIds()));

request.getItmIds has 1 million Ids due to which I am getting an exception

org.bson.BsonMaximumSizeExceededException: Document size of 46282052 is larger than maximum of 16793600

Can anyone help me with this one?


回答1:


If you are using Spring Data JPA, you can do something like: findBySomeField(String someField)

If you have a more complex query, you can actually use JPQL and write a custom query.

@Query(value = "SELECT o.* from SomeObject o WHERE :someField IS NULL OR o.someField = :somefield)
public findBySomeField(@Param("someField") String someField);


来源:https://stackoverflow.com/questions/55018869/spring-mongodb-mongotemplate-criteria-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!