Search code examples
rethinkdbfeathersjsfeathers-hook

is there any way where i can apply group and pagination using createQuery?


Query like this,

http://localhost:3030/dflowzdata?$skip=0&$group=uuid&$limit=2

and dflowzdata service contains data like,

[ { "uuid": 123456, "id": 1 }, { "uuid": 123456, "id": 2 }, { "uuid": 7890, "id": 3 }, { "uuid": 123456, "id": 4 }, { "uuid": 4567, "id": 5 } ]

Before Find Hook like,

if (query.$group !== undefined) { 
  let value = hook.params.query.$group
  delete hook.params.query.$group
  const query = hook.service.createQuery(hook.params.query);
  hook.params.rethinkdb = query.group(value)
}

Its gives correct result but without pagination, like I need only two records but its give me all records

result is,

{"total":[{"group":"123456","reduction":3},{"group":"7890","reduction":1},{"group":"4567","reduction":3}],"data":[{"group":"123456","reduction":[{"uuid":"123456","id":1},{"uuid":"123456","id":2},{"uuid":"123456","id":4}]},{"group":"7890","reduction":[{"uuid":"7890","id":3}]},{"group":"4567","reduction":[{"uuid":"4567","id":5}]}],"limit":2,"skip":0}

can anyone help me how should get correct records using $limit?


Solution

  • According to the documentation on data types, ReQL commands called on GROUPED_DATA operate on each group individually. For more details, read the group documentation. So limit won't apply to the result of group.

    The page for group tells: to operate on all the groups rather than operating on each group [...], you can use ungroup to turn a grouped stream or grouped data into an array of objects representing the groups.

    Hence ungroup to apply functions to group's result:

    r.db('db').table('table')
    .group('uuid')
    .ungroup()
    .limit(2)