Issue
I have a list of thousands ids and based on these ids I want to determine which entities exist in my DB, because the list is big I can't afford sending thousands of queries synchronously, BulkOperations
won't help me because it only supports insert,update and remove operations. I can't use findAll(Query query..)
because each entity contains massive binary fields and from what I know Spring doesn't support lazy loading in MongoDB.
What I want to achieve:
List<String> ids = getIds();
List<Boolean> existsList = repository.exists(ids);
Solution
I believe Ravi Soni's approach is the optimal approach and I'm going to show how to achieve it:
List<String> ids = getIds();
Query query = new Query(Criteria.where("_id").in(ids));
//Now we only request the _id field and leave all other fields blank
query.fields().include("_id");
List<YourClass> result = repository.findAll(query);
List<String> existingIds = result.stream().map(e -> e.getId()).collect(Collectors.toList());
Answered By - Michael Dz
Answer Checked By - Gilberto Lyons (JavaFixing Admin)