DeeDee
DeeDee

Reputation: 2751

How can I perform an eqJoin() on a deeply-nested but predictably-placed attribute?

I have a table (db name is libstats, table name is flowcells) full of documents, all of which have a structure like this:

      {
        "barcode": "C3W9UACXX",
        "id": "0021732f-2c42-4e9a-90fd-c68bb0d998dc",
        "lanes": {
            "1": [
                {
                    "bases": 2431000000,
                    "library_id": "SL58263",
                    "perc_raw_clusters": 5.5,
                    "pf_reads": 24312986,
                    "q30": 92.23,
                    "qscore": 35.82,
                    "reads": 25834646
                },
                {
                    "bases": 2121000000,
                    "library_id": "SL58264",
                    "perc_raw_clusters": 4.83,
                    "pf_reads": 21209905,
                    "q30": 91.57,
                    "qscore": 35.62,
                    "reads": 22701386
                }...],
            "2": [
                {
                    "bases": 2431000000,
                    "library_id": "SL58263",
                    "perc_raw_clusters": 5.5,
                    "pf_reads": 24312986,
                    "q30": 92.23,
                    "qscore": 35.82,
                    "reads": 25834646
                },
                {
                    "bases": 2121000000,
                    "library_id": "SL58264",
                    "perc_raw_clusters": 4.83,
                    "pf_reads": 21209905,
                    "q30": 91.57,
                    "qscore": 35.62,
                    "reads": 22701386
                }...],
         },
         //more keys
  }

The lanes object will always have the same keys (numbers 1 through 8), and the objects in the array associated with each of these keys will always have the library_id attribute. I need to add the library_name to each of these objects, such that I get a result like this:

{ ... "lanes":{ "1": [ { "bases": 2121000000, "library_id": "SL58264", "library_name": 'my_library_name', <---added attribute "perc_raw_clusters": 4.83, "pf_reads": 21209905, "q30": 91.57, "qscore": 35.62, "reads": 22701386 }, ... ],... ... {

I have another table with the library_name attribute (db name libraries, table name libraries) with a much simpler structure:

{
   library_id: 'SL123456',
   library_name: 'my_library_name'
}

Is there a RQL query I can use to accomplish this? On simpler tables, eqJoin() works beautifully, but I'm having trouble making it work with this more complex structure.

Upvotes: 0

Views: 94

Answers (1)

mlucy
mlucy

Reputation: 5289

This would be easier if lanes were an array rather than an object, but with the current document structure a query like this should do it:

r.db('libstats').table('flowcells').merge(function(flowcell) {return {
    'lanes': flowcell('lanes').keys().map(function(n) {
      return r.expr([n, flowcell('lanes')(n).merge(function(lane) {
        {'library_name': r.db('libraries').table('libraries').get(lane('library_id'))}]).coerce_to('OBJECT')})};})

Upvotes: 1

Related Questions