Batch exists query in Spring MongoDB / exists for multiple ids

Viewed 843

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

I have an option but there is probably more optimal, I did the best I could for now (beginner). You can with only one query do something like that :

  • $match:{_id:{$in:ids}} // Select all the elems matching any of id in ids
  • $project:{index:{$indexOfArray:[ids, "$_id"]} //add an index field showing you the position of each item into the ids array you are passing With those results you can retrieve which index never appear on results and so know they dont exist in your database. I know there must be simpler than that but that's all I have ^^

just fetch the id field (ignore other fields) of every document and then in your application, check which exists or not.

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