0

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?

Sambhav
  • 217
  • 2
  • 16
  • 1
    Where does `request.getItmIds()` come from? Can't it be paged so that you don't send 1 million IDs at once? – g00glen00b Mar 06 '19 at 08:51
  • Can't it be paged so that you don't send 1 million IDs at once? - This is exactly where I am stuck .. Can MongoDB query be paginated or something else.. – Sambhav Mar 06 '19 at 09:00
  • check this: https://stackoverflow.com/q/15307800/2987755 – dkb Mar 06 '19 at 09:52
  • Also: https://docs.mongodb.com/manual/core/gridfs/ – dkb Mar 06 '19 at 10:14
  • The pagination is a challenge with this approach. There is a possible solution to implement pagination but the tradeoff for that is to make two calls to the database. – Athar Apr 15 '20 at 00:01

1 Answers1

-2

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