【问题标题】:How to project array element field in Spring Data Mongo DB Aggregation如何在 Spring Data Mongodb 聚合中投影数组元素字段
【发布时间】:2018-07-30 15:31:03
【问题描述】:

如何使用下面的文档示例在 Spring Data MongoDB Aggregation 中投影嵌入的数组元素字段,我试过了:

  • project("customers.id")
  • project("customers.[].id")
  • project("customers.?.id")
  • project("$customers.id")

但没用。

没有投影的结果文档:

{
    "id": "group1",
    "name": "Default Identity Management",
    "warningThreshold": 900000,
    "tariffId": "TR_0001",
    "active": false,
    "customers": [
        {
            "id": "1",
            "name": "David",
            "properties": [
                {
                    "name": "phone",
                    "value": "678"
                }
            ],
            "roles": [
                "dev"
            ]
        },
        {
            "id": "2",
            "name": "Peter",
            "properties": [
                {
                    "name": "phone",
                    "value": "770"
                }
            ],
            "roles": [
                "techsales",
                "dev"
            ]
        }
    ]
}

预期的文件是这样的:

{
    "id" : "group1",
    "name" : "Group1",
    "tariffId" : "TR_0001",
    "warningThreshold" : 900000,
    "customers" : [
        {
            "id" : "1",
            "name" : "David",
            "properties" : [
                {
                    "name" : "phone",
                    "value" : "678"
                }
            ]
        },
        {
            "id" : "2",
            "name" : "Peter",
            "properties" : [
                {
                    "name" : "phone",
                    "value" : "770"
                }
            ]
        }
    ]
}

我想加入customers[].idcustomers[].namecustomers[].properties

【问题讨论】:

  • 通过project.andExclude("customers.roles"); 使用投影。它应该在 3.4 中使用 spring mongo 2.x jar。
  • 我在尝试使用 project("id","customers").andExclude("customers.name") 时遇到了异常:'Bad projection specification, cannot exclude fields other than '_id' in an inclusion projection
  • 是的。几件事。需要的版本是 3.4 mongo server verisin 和 2.0 spring mongo jar。您必须使用额外的项目阶段来排除。不能在单个项目中混合和匹配包含和排除。
  • 请检查版本。在 mongo shell 上运行 db.version()。
  • 对不起。我没有想到嵌​​套字段的投影在春天不起作用。你可以试试AggregationOperation project = new AggregationOperation() { @Override public Document toDocument(AggregationOperationContext aggregationOperationContext) { return new Document("$project", new Document("customers.roles", 0)); } };。使用 lambda,您可以减少到 AggregationOperation project = aggregationOperationContext -> new Document("$project", new Document("customers.roles", 0));

标签: java spring mongodb spring-data spring-data-mongodb


【解决方案1】:

我一直在尝试解决这个问题,但无法解决。 stackoverflow 上的其他帖子以及互联网上的其他地方都没有提供我正在寻找的解决方案。

我的问题与原作者的类似:有一个文档,其中有一个字段是文档数组。我想查询文档中的所有顶级字段,并从数组中的文档中排除单个字段。

s7vr 在 cmets 中对这个问题的回答为我做了这个工作!只是在这里重新发布,因为大多数人不会通过所有的 cmets,这是一个非常有用的答案,它使我免于编写很多蹩脚的代码! :D

AggregationOperation project = new AggregationOperation() {
    @Override
    public Document toDocument(AggregationOperationContext aggregationOperationContext) {
        return new Document("$project", new Document("arrayField.nestedFieldToExclude", 0));
    }
};

使用 Lambda:

AggregationOperation project = aggregationOperationContext -> new Document("$project", new Document("arrayField.nestedFieldToExclude", 0));

整体管道:

Aggregation aggregation = Aggregation.newAggregation(
    Aggregation.match(criteria),
    Aggregation.sort(Sort.Direction.DESC, "createdOn"),
    project);

我只是希望有一种更简洁的方法可以直接使用 Spring MongoDB Data API 来执行此操作,而不是使用 lambda 函数。

另外,请注意 AggregationOperation.toDocument(AggregationOperationContext aggregationOperationContext) 方法自 spring-data-mongodb 版本 2.2 起已被弃用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2018-05-06
    • 2017-01-04
    • 1970-01-01
    相关资源
    最近更新 更多