suman j
suman j

Reputation: 6980

mongodb bulk records exists validation in spring data

Given a List of mongo document UUIDs, I would like to validate if these IDs have corresponding documents in the DB. What is the efficient way to perform bulk validation? Lets say my program has 100 id strings and would like to validate all these IDs in one DB call. Is it feasible? I could do it sequentially (i.e using exists(T id) methods in spring data repository) but I would rather want to do in one call.

My program uses spring data for mongodb. But I am open to any native mongo command. I can code the Query in spring data for it. Also, If any of the ID in the list does not have a document in DB, I want to get such ID in response.

Upvotes: 1

Views: 511

Answers (1)

NG.
NG.

Reputation: 22914

I don't know spring-data but you can use the $in operator and do something like find({ _id : { $in : [ your, ids, here ] } }) and then work from there. If you need a simple yes/no for all of them existing, you can just check the length of your result set vs. the length of your input array.

Upvotes: 1

Related Questions