Reputation: 267
I am using MongoDB for my spring-boot application. I need to find the list of documents corresponding to list of IDs.
I have a Role collection and a list called roleIdList. This list (roleIdList) contains all the role ids whose documents needs to be fetched.
Below is my query:
// My roleIdList is of type List<ObjectId>. I also tried with List<String>
Query query = Query.query(where("_id").all(roleIdList));
List<RolesEntity> rolesEntityList = mongoOperations.find(query, RolesEntity.class);
But with the above query, I am getting empty rolesEntityList. Can anyone please help me.
Thanks in advance!
Upvotes: 0
Views: 1251
Reputation: 1
you can also use: List<RolesEntity> findByRole_IdIn(List<String> roleIdList)
Upvotes: 0
Reputation: 565
Use in
instead of all
Query query = Query.query(where("_id").in(roleIdList));
See:
https://docs.mongodb.com/manual/reference/operator/query/all/ https://docs.mongodb.com/manual/reference/operator/query/in/
Upvotes: 1