我将MongoDB用于春季启动应用程序。我需要找到与ID列表相对应的文档列表。
我有一个Role集合和一个名为roleIdList的列表。此列表(roleIdList(包含需要获取其文档的所有角色ID。
以下是我的查询:
// 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);
但是通过上面的查询,我得到了空的rolesEntityList。有人能帮我吗?
提前感谢!
使用in
而不是all
Query query = Query.query(where("_id").in(roleIdList));
参见:
https://docs.mongodb.com/manual/reference/operator/query/all/https://docs.mongodb.com/manual/reference/operator/query/in/
您也可以使用:List<RolesEntity> findByRole_IdIn(List<String> roleIdList)