Search code examples
rethinkdbrethinkdb-javascript

Accessing nested fields with Rethinkdb


So I have a result that looks like this

"data": {
  "randomkeyllasdkjflk": {
     "name": "John Doe"
  },
  "anotherrandomkeyadf": {
     "name": "Mona Lee"
  }
}

and I want to access the name.

This is what I tried:

r.table('users').filter(function (doc) {

      return doc('data').coerceTo('array').map(function(ref) {
        return ref('name')
      }).contains("John")

    });

But will produce an error that says:

e: Cannot perform bracket on a non-object non-sequence "randomkeyllasdkjflk"


Solution

  • use this:

    r.table('users').filter(function (doc) {
    
      return doc('data').keys().map(function(ref) {
        return doc('data')(ref)('name')
      }).contains("John")
    
    });