Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Spring Boot从MongoDB存储库中选择嵌套属性_Mongodb_Spring Boot - Fatal编程技术网

使用Spring Boot从MongoDB存储库中选择嵌套属性

使用Spring Boot从MongoDB存储库中选择嵌套属性,mongodb,spring-boot,Mongodb,Spring Boot,我喜欢在MongoRepository中解析方法名,因此不必编写查询。但是我想知道是否有一种方法可以使用这种模式只选择某个(嵌套的)字段 我的文档看起来像: @Document(collection = "elements") public class ElementEntity { @Id private String id; private String type; private MetaData metaData; private String

我喜欢在
MongoRepository
中解析方法名,因此不必编写查询。但是我想知道是否有一种方法可以使用这种模式只选择某个(嵌套的)字段

我的文档看起来像:

@Document(collection = "elements")
public class ElementEntity {

    @Id
    private String id;

    private String type;
    private MetaData metaData;
    private String json;

}

public class MetaData {

    private String title;
    private String description;
    private final List<String> keywords = new ArrayList<>();

}
@文档(collection=“elements”)
公共类元素实体{
@身份证
私有字符串id;
私有字符串类型;
私有元数据;
私有字符串json;
}
公共类元数据{
私有字符串标题;
私有字符串描述;
私有最终列表关键字=新ArrayList();
}
我可以通过以下关键字搜索ElementEntities:

List<ElementEntity> findByMetaDataKeywords(String keyword);
列出FindBymetadata关键字(字符串关键字);
现在我想得到一个可能的关键字列表,但我没有找到任何文档说明如何使用方法名模式,或者是否可以使用方法名模式。我希望这样的事情可能会奏效,但事实并非如此:

List<String> getDistinctMetaDataKeywordsAsc();
List getdistinctmetadatadatakeywordsasc();
有没有办法只用一个接口方法来实现这一点,或者我需要编写一个(SQL?)查询

根据其中一个答案中的注释进行编辑: 假设我的元素集合中有两个文档:

  • 有关键词:“免责声明”和“合法”
  • 有关键词:“脚注”和“法律”
  • 我希望有一个方法可以返回一个按字母顺序排列的三个不同关键字的列表:“免责声明”、“脚注”、“法律”

    试试这个

           List<ElementEntity> findByMetaData_Keywords(List<String> keywords);
    
    List findByMetaData_关键字(List关键字);
    
    这不是我想要的,更确切地说,这是如上所述的工作方式。我想获得所有元素的所有可能关键字。你能在你的问题中添加你的预期输出吗?我添加了一些关于我试图实现的目标的更多解释。眼睛,我尝试了许多不同的“查找”或“获取”组合。Spring然后抛出如下消息:找不到类型ElementEntity的属性findDistinctMetaDataKeywords!如果我在方法中的某处添加,它需要一个参数:无效的参数索引!您似乎声明的查询方法参数太少!]根本原因是您必须使用mongo template
    mongoTemplate.getCollection(COLLECTION).distinct(关键字)