rethinkdbrethinkdb-javascript

Condition inside getAll for compound index


what will be the equivalent query for

select * from emails where to="someemail" and from="some@email"

need to use get instead of filter


Solution

  • getAll is your friend:

    table.getAll([key, key2...], [, {index:'id'}]) → selection

    Assuming that you have a compound key index, something like:

    r.db('emails')
        .table('emails')
        .indexCreate('conversation', [r.row('from'), r.row('to')])
    

    you can easily get all emails:

    r.db('emails')
        .table('emails')
        .getAll(['some@email', 'someemail'], {index: 'conversation'})
    

    For example, having the following dataset

    r.db('emails')
        .table('emails')
        .insert([
            {from: 'foo@mail', to: 'bar@mail', text: 'Hi Bar!'},
            {from: 'bar@mail', to: 'foo@mail', text: 'Hi Foo!'},
            {from: 'foo@mail', to: 'bar@mail', text: 'Bye Bar!'},
            {from: 'bar@mail', to: 'foo@mail', text: 'Bye Foo!'}
        ])
    

    that's queried with the following query

    r.db('emails')
       .table('emails')
       .getAll(['foo@mail', 'bar@mail'], {index: 'conversation'})
       .pluck('text')
    

    will produce:

    [
        {"text": "Bye Bar!"},
        {"text": "Hi Bar!"}
    ]
    

    (The order above is undefined.)