Java 如何正确查询多个嵌套数组以获得字符串匹配蒙哥达

Java 如何正确查询多个嵌套数组以获得字符串匹配蒙哥达,java,spring,mongodb,spring-boot,mongodb-query,Java,Spring,Mongodb,Spring Boot,Mongodb Query,我正在尝试构建一个MongoDB查询,在该查询中,我可以搜索一个对象列表,其中所有对象都包含多个嵌套的对象数组,其中最低级别的属性包含字符串的一部分 这是这些对象的一般结构: { "name": "Foo" "structure": { "configs":[ { "combinations": [ { "title": "item1"

我正在尝试构建一个MongoDB查询,在该查询中,我可以搜索一个对象列表,其中所有对象都包含多个嵌套的对象数组,其中最低级别的属性包含字符串的一部分

这是这些对象的一般结构:

{
   "name": "Foo"
   "structure": {
       "configs":[
          {
            "combinations": [
                {
                  "title": "item1"
                },
                {
                  "title": "item2"
                }
            ]
          },
          {
            "combinations": [
                {
                  "title": "item3"
                },
                {
                  "title": "item4"
                }
            ]
          }
       ]
   }
}
现在,当我搜索“item1”或仅搜索“1”时,我希望返回该示例对象,因为第一个
combinations
数组包含一个标题为
item1
的对象。 由于我是在Spring boot中构建应用程序的,通常查询可以通过repository类中常见的高级
findAllByPropertyMatching(String searchTerm)
轻松处理。由于它的复杂性,这在这种情况下不起作用,我真的很挣扎如何去做这件事

我尝试了一个自定义查询

@Query(value = "{'structure.configs.$[].combination.$[].title': {$regex : ?0, $options: 'i'}}")
public List<Item> findAllByStructuregMatchesRegex(String query);
@Query(value=“{'structure.configs.$[].composition.$[].title':{$regex:?0,$options:'i'}”)
公共列表FindAllSystemStructureMatchesRegex(字符串查询);
…但显然,它有多个问题

因为数据是从外部源加载的,所以我无法更改底层数据结构。我也不能缓存所有项目并用Java逻辑进行过滤,因为数据集太大了


谁能给我指出正确的方向吗?我非常感谢你的帮助,非常感谢

经过更多的尝试和错误,解决方案似乎非常简单:

 @Query(value = "{'structure.configs': {\n" +
            "            '$elemMatch': {\n" +
            "                'combinations': {\n" +
            "                    '$elemMatch': {\n" +
            "                        'title': {$regex : ?0, $options: 'i'}\n" +
            "                    }\n" +
            "                }\n" +
            "            }\n" +
            "        }}")
    public List<Item> findAllByStructureMatchesRegex(String query);
@Query(value=“{'structure.configs':{\n”+
“'$elemMatch':{\n”+
“‘组合’:{\n”+
“'$elemMatch':{\n”+
'title':{$regex:?0,$options:'i'}\n+
“}\n”+
“}\n”+
“}\n”+
"        }}")
公共列表findAllByStructureMatchesRegex(字符串查询);