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

不想你离开。 提交于 2019-12-23 06:27:23

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/52537063/is-there-any-way-where-i-can-apply-group-and-pagination-using-createquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!