java - spring-data-mongodb:如何通过传递随机属性及其值来查找文档

标签 java json spring mongodb spring-data-mongodb

我的 mongodb 中有以下文档。

{
    "_id" : "840e922e-05e0-4e4d-b574-303a72425bdd",
    "_class" : "com.document.domain.Doc",
    "type" : "User",
    "name" : "steven",
    "index" : 0,
    "data" : "This is sample user",
    "properties" : {
        "displayName" : "steven",
        "lastName" : "smith"
    },
    "tags" : [ 
        "tag1", 
        "tag2"
    ],
    "categories" : [ 
        "category1", 
        "category2"
    ]
}

现在我想通过传递上面 json 中的任何随机属性来检索文档。

Example: If I pass "type" as a key and "User" as value
         If I pass "name" as a key and "steven" as value

就像这个键可以是 JSON 中的任何随机属性,值将是其关联值,那么它应该返回该文档。

我正在尝试以下查询:

@Query("{'property':?0,'property':?1}")
List<Doc> findByKeyAndValue(String key, String value);

但运气不佳。

提前致谢。

最佳答案

尝试更改 placeholders 允许您将方法参数中的键和值替换为 JSON 查询字符串,其中 ?0 是键的占位符,?1 是值(value)。例如,

@Query("{?0:?1}")
List<Doc> findByKeyAndValue(String key, String value);

关于java - spring-data-mongodb:如何通过传递随机属性及其值来查找文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30753945/

相关文章:

java - 将 scan.nextLine 的结果与 if-else 语句结合使用 — Java

java - 使用 Java EE 实现聊天应用程序

javascript - 利用异步 Json 请求/方法调用来加快页面加载速度

spring - 使用 OAuth2 资源所有者密码授权类型在 Spring Cloud Gateway 中创建路由

java - Spring Security SAML + HTTPS 到另一个页面

spring - Cordova POST - 请求禁止 403。未到达 Dispatcher Servlet

java - Android - 使用快照遍历firebase数据库

java - 如何在 IntelliJ IDEA 中使用新的 Application Run 配置 UI 包含提供的依赖项?

jquery - 获取范围之外的 JSON 数据

java - 以列表形式获取 JSON 键值的方法