【问题标题】:Spring data MongoDb Aggregation lookup cascadingSpring数据MongoDb聚合查找级联
【发布时间】:2020-05-23 19:50:33
【问题描述】:

我正在使用 Spring (boot) 2.2.7 和在 Debian Stretch (9.12) 上运行的 mongoDB 4.0。 我已经设置了 3 个我试图通过聚合查找级联操作加入的集合。

  • 操作(节点类)
  • 库存(组件类)
  • 目录(产品类)

每个类都有一个将它们链接在一起的 uuid 字符串属性。

节点(传感器、接收器、基站等)

@Document(collection = "operations")
public class Node implements Serializable {
    @Id
    private String _id;
    private String componentUuId;
....
}

组件(带有序列号的交付产品,裸码...)

@Document(collection = "stock")
public class Component implements Serializable {
    @Id
    private String _id;
    private String uuId;
    private String productUuId;
....
}

产品(市场人工制品)

@Document(collection = "catalog")
public class Product implements Serializable {
    @Id
    private String _id;
    private String uuId;
....
}

在 mongo shell 中运行此命令时:

db.operations.aggregate([
   {
     $lookup: {
         "from": "stock",
         "localField": "componentUuId",
         "foreignField": "uuId",
         "as": "component"
     }
   },
   {
     $lookup: {
         "from": "catalog",
         "localField": "component.productUuId",
         "foreignField": "uuId",
         "as": "product"
     }
   }
]).pretty()

我明白了

{
    "_id" : ObjectId("5ec952a7714e7e248b5fb98b"),
    "uuId" : "3b5e3417-166c-4d69-9fa5-b0856d819541",
    "componentUuId" : "57eff5c5-8b3c-43cb-9f2d-1c4098fc0c1f",
    "component" : [
        {
            "_id" : ObjectId("5ec952a4714e7e248b5fb97f"),
            "uuId" : "57eff5c5-8b3c-43cb-9f2d-1c4098fc0c1f",
            "productUuId" : "3da7a6a6-ccf6-4fe1-892a-a6d6b82396c1",
            "created" : ISODate("2020-05-23T14:43:16.588Z"),
            "_class" : "org.open_si.udc_common.models.Component"
            .......
        }
    ],
    "product" : [
        {
            "_id" : ObjectId("5ec952a1714e7e248b5fb969"),
            "uuId" : "3da7a6a6-ccf6-4fe1-892a-a6d6b82396c1",
            "model" : "TMP36GT9Z",
            "_class" : "org.open_si.udc_common.models.Product"
            ......
        }
    ]
}

为了在 Java 中实现相同的目标(使用 Spring-data-mongodb),我跑了

Aggregation agg = Aggregation.newAggregation(
    Aggregation.lookup(mongoTemplate.getCollectionName(Component.class),
        "componentUuId", "uuId", "component"),
    Aggregation.lookup(mongoTemplate.getCollectionName(Product.class),
        "component.productUuId", "uuId", "product")
);

AggregationResults<NodeExpanded> results = mongoTemplate.aggregate(agg, mongoTemplate.getCollectionName(Node.class), NodeExpanded.class);

return results.getMappedResults();

使用 NodeExpanded 类

public class NodeExpanded implements Serializable {

    private String uuId;
    private Component component;
    private Product product;
....
}

注意:我没有扩展 Node 基类,因为在出​​现以下问题之前出现重复键错误。

问题是我在托管该代码的服务中遇到错误:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null

不幸的是,我不知道如何在 Spring 的较低级别进行调试,也无法找出原始错误是什么。

请注意,获取链接到 Component 的 Product 的代码可以正常工作:

Aggregation agg = Aggregation.newAggregation(
                Aggregation.lookup(mongoTemplate.getCollectionName(Product.class),
                        "productUuId", "uuId", "product")
        );

AggregationResults<ComponentExpanded> results =
                mongoTemplate.aggregate(agg,mongoTemplate.getCollectionName(Component.class), ComponentExpanded.class);
return results.getMappedResults();

使用扩展类:

public class ComponentExpanded implements Serializable {
    private String uuId;
    private Product product;
...
}

有什么想法吗?提前致谢。


编辑

为了更深入地调试,我将代码限制为仅将 operations 集合(Node.class)加入到 stock 中的一个(Component.class)我对 stock (Component.class) 和 catalog (Product.class) 的处理方式相同。

public List<NodeExpanded> list() {

    Aggregation agg = Aggregation.newAggregation(
            Aggregation.lookup(mongoTemplate.getCollectionName(Component.class),
                    "componentUuId", "uuId", "component")

    );

    AggregationResults<NodeExpanded> results = mongoTemplate.aggregate(agg, mongoTemplate.getCollectionName(Node.class), NodeExpanded.class);
    return results.getMappedResults();
}

用一个简单的 NodeExpanded 类作为

public class NodeExpanded implements Serializable {

    private String uuId;
    private Component component;

}

我在 mongoTemplate.aggregate() 指令上得到相同的空指针异常


已解决

问题来自没有 @Autowired MongoTemplate 的 SensorPersistenceService。 根据该帖子中提供的信息,无法找出错误原因。 再次感谢@Valijon 在此任务中提供的帮助^^

【问题讨论】:

  • 看来你初始化错误SensorPersistenceService。尝试将以..Service、Repository 和@Autowired 结尾的属性标注。
  • 去试试。我不自动装配服务,因为它们被注入到控制器构造器中。只有存储库是。
  • 你是对的,不是关于应用程序服务/存储库,而是关于在 SensorPersistenceService 中不是 @Autowired 的 MongoTemplate。

标签: mongodb spring-boot aggregation-framework cascade lookup-tables


【解决方案1】:

问题可能是NodeExpanded 期望单个Component 和Product,但$lookup 聚合为两者返回array。

如果您添加额外的$project 阶段,它应该可以解决您的问题。

Aggregation agg = Aggregation.newAggregation(
    Aggregation.lookup(mongoTemplate.getCollectionName(Component.class), 
        "componentUuId", "uuId", "component"),
    Aggregation.lookup(mongoTemplate.getCollectionName(Product.class), 
        "component.productUuId", "uuId", "product"),
    Aggregation.project()
        .andInclude("uuId") //put here all fields for NodeExpanded
        .and(ArrayOperators.ArrayElemAt.arrayOf("component").elementAt(0)).as("component")
        .and(ArrayOperators.ArrayElemAt.arrayOf("product").elementAt(0)).as("product")
);

【讨论】:

  • 再次感谢您的帮助@Valijon。不幸的是,我认为这不是最初的问题。我测试了一个非常简单的代码(见我的帖子编辑),它仍然会导致空指针异常。
  • @EmmanuelBRUNET 你能分享完整的代码/数据吗? $lookup 返回一个数组,但你的类只有一个字段值时,不可能正常工作。
  • 当然 ... 通用类位于 [common] 项目和 [gateway] 项目中的前端控制器。由于我是新手,请随时给我建议,在此先感谢。您可以在gitlab.open-si.org 找到整个代码(帐户:guest - pwd:grenoble2020)
  • 注意:mongoDb数据可以使用模拟器项目加载(见模拟器/wiki/用法)
  • 由于我从 mongoDb 导出数据时遇到了一些解析问题,模拟器项目/wiki/数据库配置集合示例中有一个基本的数据库配置数据
猜你喜欢
  • 2020-09-09
  • 2022-01-03
  • 2017-07-19
  • 1970-01-01
  • 2021-09-03
  • 1970-01-01
  • 1970-01-01
  • 2017-07-14
  • 1970-01-01
相关资源
最近更新 更多