如何使用SpringBoot从MongoDB中筛选子文档?

huangapple go评论58阅读模式
英文:

How can I filter sub-documents from MongoDB with SpringBoot?

问题

以下是翻译好的部分:

我在一个集合中有这样的文档:

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

我检索所有匹配 login="xxx" 且 subDocNestedArray 包含 "AAAA" 的文档,但只希望返回与根文档的其他属性匹配的子文档,例如:

{
  "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 查询返回经过过滤的结果。

英文:

I have documents in a collection such as :

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

I retrieve all documents that match login="xxx" and subDocNestedArray="AAAA" but want only subDocs matching along other properties of the root document, ex :

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

I have tried with :

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

that results in missing fields

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

I did not manage to find the syntax using "redact".

Any clue ?

Make my MongoDB query return filtered results

答案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数组。请尝试一下。

英文:

You can try this:

MatchOperation match = match(Criteria.where(&quot;login&quot;).is(&quot;xxx&quot;).and(&quot;subDocs.subDocNestedArray&quot;).is(&quot;AAAA&quot;));
AddFieldsOperation addField = AddFieldsOperation.addField(&quot;subDocs&quot;).withValueOf(ArrayOperators.Filter.filter(&quot;subDocs&quot;).as(&quot;item&quot;).by(ArrayOperators.In.arrayOf(&quot;$$item.subDocNestedArray&quot;).containsValue(&quot;AAAA&quot;)));
Aggregation agg = Aggregation.newAggregation(match, addField);
AggregationResults&lt;XYZClass&gt; aggResults = mongoTemplate.aggregate(agg, &quot;CollectionName&quot;,XYZClass.class);

In this, we have a $match stage to filter documents, and then an addFields stage, to filter the subDocs array. Please test it out.

huangapple
  • 本文由 发表于 2023年6月12日 22:12:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定