立方体 Js | 如何连接从 MongoDB 数组创建的两个表?

Eli*_* L. 3 mongodb cube.js

我是cube.js 的新手,我遇到了一个问题,也许有人可以帮助我。我在互联网上没有找到任何非常有用的东西...这是我收集的 o 文档的示例:

{ 
    "_id" : ObjectId("5a835e0000f73b69c100f15c"), 
    "studyDescription" : "xxxxxxxx", 
    "observations" : [
        {
            "_id" : "1JELIZY6QSAGW",  
            "state" : "validated", 
            "stateBy" : "user@xxx.com", 
            "stateAt" : ISODate("2019-10-22T15:06:48.133+0000"), 
            "created" : ISODate("2019-10-22T15:06:48.133+0000"), 
            "createdBy" : "user@xxx.com", 
            "history" : [
                {
                    "author" : "user@xxx.com", 
                    "role" : "ADMIN", 
                    "state" : "validated", 
                    "tsp" : ISODate("2019-10-22T15:06:48.133+0000")
                }
            ]
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我的收藏包含研究,每项研究都包含一些观察结果。每个观察结果都可以由一个或多个审阅者审阅,并且该信息包含在“历史”数组中。我需要做一些报告,所以建议我使用cube.js。问题是我需要使用数组中包含的数据过滤一些图表,为此,我需要进行一些联接。我的问题是“观察”数组不包含研究 id,“历史”数组既不包含研究 id 也不包含观察 id,而我需要两者连接表并根据作者进行过滤。我无法加入他们,除非我修改数据库中的集合以添加信息,不幸的是,这在我的情况下不是一个选项......

您有想法让这种加入成为可能吗?

非常感谢您的帮助

小智 5

嵌入文档由 Mongo BI 连接器表示为相关表。在您的情况下,将有下表:

  1. 学习
  2. 研究观察
  3. 研究观察历史

在这种情况下,Cube.js 架构将如下所示:

cube(`Studies`, {
  sql: `select * from studies`,

  joins: {
    Observations: {
      sql: `${Studies}._id = ${Observations}._id`,
      relationship: `hasMany`
    }
  },

  measures: {
    count: {
      type: `count`
    }
  },

  dimensions: {
    id: {
      sql: `_id`,
      type: `string`,
      primaryKey: true
    }
  }
});

cube(`Observations`, {
  sql: `select * from studies_observations`,

  joins: {
    History: {
      sql: `${Observations}._id = ${History}._id AND ${Observations}.observations_idx = ${History}.observations_idx`,
      relationship: `hasMany`
    }
  },

  dimensions: {
    id: {
      sql: `CONCAT(${CUBE}._id, ${CUBE}.observations_idx)`,
      type: `string`,
      primaryKey: true
    }
  }
});

cube(`History`, {
  sql: `select * from studies_observations_history`,

  dimensions: {
    id: {
      sql: `CONCAT(${CUBE}._id, ${CUBE}.observations_idx, ${CUBE}.\`observations.history_idx\`)`,
      type: `string`,
      primaryKey: true
    },

    author: {
      sql: `${CUBE}.\`observations.history.author\``
    }
  }
})
Run Code Online (Sandbox Code Playgroud)

了解有关Mongo BI 数组架构Cube.js 连接的更多信息。