3

My first concatMap return something like following. I would like to get the value of "d" so the value will be "Rethinkdb"

 [  
       {  
          "a":{  
             "b":{  
                "c":"NoSQL",
                "d":"Rethinkdb"
             }
          }
       },
       {  
          "a":{  
             "b":"text"
          }
       },
       {  
          "a":{  
             "b":"another"
          }
       }
    ]

I tried like r.(...).concatMap(function(f){return f("a")("b")("d")}) But display error RqlRuntimeError: Cannot perform bracket on a non-object non-sequence (This is because in 2nd and 3rd "b" there is no "d"). I cannot use nth because "d" will not be always at first element of array.

Prakash Thapa
  • 1,285
  • 14
  • 27

1 Answers1

2

You should take a look at the hasFields method and it's ability to check for the existence of nested properties using a boolean. There's a very good example of this scenario in the docs.

I would get all the documents that have the fields you want first and then do the concatMap.

r.table('')
 // Get all documents with a value in property in `a.b.d`
 // `true` here doesn't refer to the value of the property, 
 // but to its existence
 .hasFields({ a: { b: { d: true } } })
 .concatMap(function (row) {
   return [ row('a')('b')('d') ];
 })
Jorge Silva
  • 4,574
  • 1
  • 23
  • 42
  • Thanks, it works perfectly. You understood what I exactly want, if question is not(for others), You are free to edit it. Thanks again. – Prakash Thapa Apr 19 '15 at 07:55