getAll中的条件为复合索引

时间:2018-04-19 21:01:30

标签: rethinkdb rethinkdb-javascript

的等效查询
select * from emails where to="someemail" and from="some@email"

需要使用get而不是filter

1 个答案:

答案 0 :(得分:1)

DICOM Standard, Part 3, C.11.2是你的朋友:

  

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

假设您有复合键索引,例如:

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

您可以轻松获取所有电子邮件:

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

例如,拥有以下数据集

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!'}
    ])

使用以下查询查询

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

将产生:

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

(上述顺序未定义。)