E.Im
E.Im

Reputation: 3

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})

Upvotes: 0

Views: 33

Answers (2)

Eduardo Gonzalo
Eduardo Gonzalo

Reputation: 38

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

Upvotes: 1

dalanmiller
dalanmiller

Reputation: 3672

I assume what you mean is "only the first index in the embedded array" and that article_id: 678 would be one of the key, value pairs embedded in the optionList:

r.db('myDataBase').table('myTable')('optionList').filter({article_id:678})

And then for the other result:

r.db('myDataBase').table('myTable')('array').nth(0)

Upvotes: 0

Related Questions