【问题标题】:How to use Criteria all in querying mongoDb in java?java - 如何在java中查询mongoDb时使用Criteria all?
【发布时间】:2022-01-18 12:39:22
【问题描述】:

我将 MongoDB 用于我的 spring-boot 应用程序。我需要找到与 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);

但是通过上面的查询,我得到了空的角色实体列表。谁能帮帮我。

提前致谢!

【问题讨论】:

    标签: mongodb spring-boot mongodb-query


    【解决方案1】:

    使用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/

    【讨论】:

      【解决方案2】:

      您也可以使用:List&lt;RolesEntity&gt; findByRole_IdIn(List&lt;String&gt; roleIdList)

      【讨论】:

        猜你喜欢
        • 2011-08-02
        • 2010-09-10
        • 1970-01-01
        • 2017-10-25
        • 2016-02-29
        • 1970-01-01
        • 2019-04-15
        • 2019-07-27
        • 1970-01-01
        相关资源
        最近更新 更多