Home > Software design >  How to use Criteria all in quering mongoDb in java?
How to use Criteria all in quering mongoDb in java?

Time:12-16

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!

CodePudding user response:

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/

CodePudding user response:

you can also use: List<RolesEntity> findByRole_IdIn(List<String> roleIdList)

  • Related