简体   繁体   中英

Cube Js | How to join two tables created from MongoDB arrays?

I am new to cube.js and I am facing a problem, maybe someone will be able to help me. I did not find anything very helpful on the internet... Here is an example of o document in my collection:

{ 
    "_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")
                }
            ]
        }
    ]
}

My collection contains studies and each study contains several observations. Each observation can be reviewed by one or several reviewers and this information is contained in "history" array. I need to do some reporting so I was advised to use cube.js. Problem is I need to filter some of my charts with data contained in arrays and to do so, i need to do some joins. My problem is that the "observations" array does not contain the study id and the "history" array does not contain either study id nor observations id whereas I would need both to join the tables and filter according to the author for example. I can't join them except if I modify the collection in the database to add the information, which is unfortunately not an option in my case...

Would you have an idea to make this join possible ?

Thank you very much for your help

Embedded documents represented as related tables by Mongo BI connector. In your case there will be following tables:

  1. studies
  2. studies_observations
  3. studies_observations_history

In this case Cube.js schema will look as follows:

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\``
    }
  }
})

Learn more about Mongo BI arrays schema and Cube.js joins .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM