How can I find the value of nested values โ€‹โ€‹of only field data in rethinkDB?

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

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

      

I've tried like r.(...).concatMap(function(f){return f("a")("b")("d")})

But the displayed error is RqlRuntimeError: Can't execute parentheses for non-sequence non-objects (that's because there is no "d" in 2nd and 3rd "b"). I cannot use nth

because "d" will not always be the first element of the array.

+3


source to share


1 answer


You should take a look at the method hasFields

and check the possibility of nested properties using boolean. There's a very good example of this scenario in the docs.

I would get all the documents that have the fields I want and then do 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') ];
 })

      

+2


source







All Articles