0

This is a sample row in my RethinkDB table.

 {
  "a1":  "val1" ,
  "a2":  "val2" ,
  "a3":  "val3" ,
  "a4":  "val4" ,
  "part": [
  {
  "id":  "reql" ,
  "position":  "student"
  } ,
 {
 "id":  "sdsadda" ,
 "position":  "officer"
 }
 ] ,
 "a5":  "val5" 

 }

I want to access a nested json object but I get the error e: Cannot perform bracket on a non-object non-sequence "string" I need the entire row in the output for rows matching id to "reql" This is my query

r.db('dbname').table('tablename').filter(r.row('part').contains(function(product) { return product('id').eq("reql"); }))

This query worked before .It doesn't right now.

dalanmiller
  • 3,467
  • 5
  • 31
  • 38
Puja
  • 17
  • 5

2 Answers2

1

You'd get that error if you'd somehow ended up with an element in your part array that's a string instead of an object. Try running .filter(r.row('part').contains(function(product) { return product.typeOf().ne('OBJECT'); }), that should return all the rows that have a string in the part array.

mlucy
  • 5,249
  • 1
  • 17
  • 21
  • Is it possible to write a query which gives results in spite of id being string in some row and object in all others? – Puja Oct 27 '15 at 21:55
  • You could replace the body of your `contains` with something like `product.typeOf().eq('OBJECT').and(product('id').eq('reql'))`. – mlucy Oct 27 '15 at 22:05
0

Regarding your comment @Puja, I think this should do it for you:

r.db('dbname').table('tablename').filter(function(d){
    d("part").typeOf().eq("ARRAY");
}).filter(r.row('part').contains(function(d) {
    return d('id').eq("reql");
}))

Although, this is less efficient than @mlucy's answer, and you should definitely just do the one pass over your dataset to clean it up by fixing all the documents where part: STRING.

dalanmiller
  • 3,467
  • 5
  • 31
  • 38