Search code examples
jsonrethinkdbrethinkdb-javascript

formulate query in rethinkdb


I have the next document in Rethinkdb.

{
  "id": 678 ,
  "author": 0 ,
  "body":  "<p>text</p>" ,
  "category":  "category" ,
  "optionlist": {
                  "option_A": "text 1" ,
                  "option_B": "text 2" ,
                  "option_C": "text 3" 
                } ,
  "rank": 4 ,
  "array": [
             "tag 1" ,
             "tag 2"
           ] ,
}

I require a query in rethinkdb where you can get as an exit only this:

"option_A": "text 1" 

and another query where you can get as an exit only this:

"tag 1"

Something like this?

r.db('myDataBase').table('myTable').pluck('id','optionlist').filter({article_id:678})

Solution

  • Ok, assuming there're 2 different requests, on the first one you'd have:

    r.db('your_db').table('your_table').filter({id:678})('optionlist').pluck('option_A')
    

    Obs: If you have the 'id' of the document, it's better to use .get() instead of .filter(), like this:

    r.db('your_db').table('your_table').get(678)('optionlist').pluck('option_A')
    

    On the second request, you'd have:

    r.db('your_db').table('your_table').get(678)('array').nth(0)
    

    Hope it helps! Cheers