如何在SpringBoot中使用MongoDB过滤子文档?

3

我有一些收藏的文件,例如:

{
  "login":"xxx",
  "someAttribute":"someValue",
  "subDocs":[
    {
      "data": "val1",
      "subDocNestedArray":["WWW","AAAA"]
    },
    {
      "data": "val2",
      "subDocNestedArray":["AAAA"]
    },
    {
      "data": "val3",
      "subDocNestedArray":["ZZZ"]
    }
    ]
}

我检索与login="xxx"和subDocNestedArray="AAAA"匹配的所有文档,但只想要与根文档的其他属性相匹配的subDocs,例如:

{
  "login":"xxx",
  "someAttribute":"someValue",
  "subDocs":[
    {
      "data": "val1",
      "subDocNestedArray":["WWW","AAAA"]
    },
    {
      "data": "val2",
      "subDocNestedArray":["AAAA"]
    }
    ]
}

我已经尝试过:

Aggregation.unwind("$subDocs"),
Aggregation.match(Criteria.where("subDocs.subDocNestedArray").is("AAAA")),
Aggregation.group("$_id")

导致缺少字段的

结果

{
  "login": null,
  "someAttribute": null,
  "subDocs":[
    {
      "data": "val1",
      "subDocNestedArray":["WWW","AAAA"]
    },
    {
      "data": "val2",
      "subDocNestedArray":["AAAA"]
    }
    ]
}

我找不到使用"redact"的语法。
有什么线索吗?
让我的MongoDB查询返回过滤后的结果。

$unwind and $group are two important stages in MongoDB's aggregation pipeline. The purpose of $unwind is to deconstruct an array field from the input documents, creating a new document for each element in the array. This allows you to perform further operations on individual elements of the array.On the other hand, $group is used to group documents together based on a specified key. It allows you to perform various aggregation operations, such as calculating the sum, average, or count of certain fields within each group.Both $unwind and $group are powerful tools that can be used independently or in combination with other stages in the aggregation pipeline. However, it is important to note that the desired results may not be achieved if you omit these stages completely.In summary, $unwind and $group play crucial roles in manipulating and analyzing data in MongoDB. They provide flexibility and control over the aggregation process, allowing you to extract meaningful insights from your data. - user20042973
1
哦,我明白了。你基本上想在Spring Boot中使用$filter - user20042973
1个回答

1
您可以尝试这个:
MatchOperation match = match(Criteria.where("login").is("xxx").and("subDocs.subDocNestedArray").is("AAAA"));
AddFieldsOperation addField = AddFieldsOperation.addField("subDocs").withValueOf(ArrayOperators.Filter.filter("subDocs").as("item").by(ArrayOperators.In.arrayOf("$$item.subDocNestedArray").containsValue("AAAA")));
Aggregation agg = Aggregation.newAggregation(match, addField);
AggregationResults<XYZClass> aggResults = mongoTemplate.aggregate(agg, "CollectionName",XYZClass.class);

在这个例子中,我们使用$match阶段来过滤文档,然后使用addFields阶段来过滤subDocs数组。请进行测试。

工作正常,谢谢。 我发现了Filter操作符,但是不理解它的语法。对于AddFieldsOperation部分,我一点都不了解。 - pibou
工作得很好,谢谢。 我发现了Filter操作符,但是不理解它的语法。我对AddFieldsOperation部分一无所知。 - undefined
1
Filter创建一个表达式,而AddFields是一个聚合阶段,它可以向文档中添加新字段或更新现有字段。@pibou - Charchit Kapoor
1
Filter创建一个表达式,而AddFields是一个聚合阶段,它可以向文档中添加新字段或更新现有字段。@pibou - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接