PouchDB: filtering, ordering and paging
Asked Answered
D

0

4

Very similar to these two CouchDB questions: 3311225 and 8924793, except that these approaches don't allow partial matching. Having e.g. these entries:

[{_id: 1, status: 'NEW', name: 'a'},
{_id: 2, status: 'NEW', name: 'aab'},
{_id: 3, status: 'NEW', name: 'ab'},
{_id: 4, status: 'NEW', name: 'aaa'},
{_id: 5, status: 'NEW', name: 'aa'}]

and key

[status, name, _id]

There seems to be no way to

  1. filter these entries by status (full string match) and name (partial string match ~ startsWith)
  2. order them by id
  3. paginate them

because of the partial string match on name. The high value unicode character \uffff that allows this partial match also causes to ignore the _id part of the key, meaning the resulting entries are not sorted by _id, but rather by status and name.

var status = 'NEW';
var name = 'aa'
var query = {
    startkey: [status, name],
    endkey: [status, name + '\uffff', {}],
    skip: 0,
    limit: 10
};

results in

[{_id: 5, status: 'NEW', name: 'aa'},
{_id: 4, status: 'NEW', name: 'aaa'},
{_id: 2, status: 'NEW', name: 'aab'}]

There is no option to sort in memory, as this would only sort the individual pages, and not the entire data set. Any ideas about this?

Dyslogia answered 12/1, 2017 at 8:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.