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

spring-data-mongodb: how to find document by passing random attribute and its value

我的 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 的任何随机属性,值将是它的关联值,那么它应该 return 我那个文件。

我正在尝试以下查询:

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

但运气不好。

提前致谢。

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

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