Search code examples
rethinkdbreql

How to find value of nested Value only field data in rethinkDB?


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.


Solution

  • 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') ];
     })