Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
This is something that has been floating around in my personal code library for a while: a MongoDB version of pager_query(). Pretty useful :) I've implemented it on the Mongo watchdog page for starters.
Comment | File | Size | Author |
---|---|---|---|
#17 | 1005790-mongodb_pager-9.patch | 4.71 KB | fgm |
#16 | 1005790-mongodb_pager-6.patch | 1.47 KB | fgm |
#13 | 1005790-mongodb_pager-2.patch | 9.36 KB | fgm |
#2 | mongo-pager-D7.patch | 4.33 KB | carlos8f |
mongodb-pager.patch | 2.8 KB | carlos8f | |
Comments
Comment #1
carlos8f CreditAttribution: carlos8f commentedNote that the watchdog table has no pager in the 6.x branch, and in HEAD there is this mess in mongodb_watchdog_overview():
Could be cleaned up nicely with this helper. Will port to D7 as well.
Comment #2
carlos8f CreditAttribution: carlos8f commentedPorted to HEAD.
Comment #3
carlos8f CreditAttribution: carlos8f commentedmongodb_watchdog_pager_init() can be removed, too.
Comment #4
carlos8f CreditAttribution: carlos8f commentedWhoops, I forgot to implement $sort.
Comment #5
thebuckst0p CreditAttribution: thebuckst0p commentedAn alternative approach which I'm using involves setting the limit() and skip() on the query separately, then using the query cursor to set the global pager variables, like so:
Comment #6
thebuckst0p CreditAttribution: thebuckst0p commentedComing back to this after a while - my function was missing a critical line for
$page
. Corrected:Comment #7
Alexander Matveev CreditAttribution: Alexander Matveev commentedHow to use this in some custom module?
I'm using some about code written below: I see pager and docs list, but the list is always the same, there is no reaction to pager surfing..
Comment #8
khayong CreditAttribution: khayong commentedI'm using object oriented way to handle paging. It is just a wrapper class to a collection, just like the
PagerDefault
class in drupal. User just have to call single line of code as below$collection = new mongoPagerCollection($collection, 50);
Comment #9
khayong CreditAttribution: khayong commentedI also created a sorting class like
TableSort
, to handle header sorting in the table. User just have to call$collection = new mongoTableSortCollection($collection, $header);
Here is the code of the class
Note that if you want to have both pager and sorting together, you will need to wrapped in
mongoTableSortCollection
first, then only wrapped inmongoPagerCollection
. Otherwise, the speed will be a bit slow. Here are the sample of the codeComment #10
mcabalaji CreditAttribution: mcabalaji commentedTried the same mentioned in #1005790-9: Mongo alternative to pager_query() and does not seem to work.
The pager_total variable is getting cleared before pager.inc. There by making pager not showing up in the page.
Do let me know anything I have to add apart from the mentioned item.
Comment #11
fgmJust a passing note : using skip() is not a good practice for pagers. In most cases, it will force the DB server to build the list of documents, browse it until the skip count, and return the rest.
What you want instead is order on something indexed and query using an expected value on this indexed sort : that way the system needn't browse from the start of the matching rows. This is explained in other words on http://docs.mongodb.org/manual/reference/method/cursor.skip/
Comment #12
Rizwan Siddiquee CreditAttribution: Rizwan Siddiquee commentedglobal $pager_page_array, $pager_total, $pager_total_items, $pager_limits;
$per_page = 10;
$page = isset($_GET['page']) ? $_GET['page'] : '';
$pager_page_array = explode(',', $page);
$on_page = $pager_page_array[0];
// result query
$result1 = $res->find(array("exam_nid" => (int)$exam_id))->limit($per_page)->skip($on_page * $per_page);
$form['path']['table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $row,
'#empty' => t('Table has no row!')
);
// Add the pager.
if ($on_page > 0 || count($row) >= $per_page) {
$pager_total_items[0] = $result1->count();
$pager_total[0] = ceil($pager_total_items[0] / $per_page);
$pager_page_array[0] = max(0, min((int) $pager_page_array[0], ((int)$pager_total[0]) - 1));
$pager_limits[0] = $per_page;
$form['path']['pager'] = array(
'#theme' => 'pager',
);
}
Comment #13
fgmRerolled the initial patch by carlos8f on today's HEAD.
Comment #14
carlos8f CreditAttribution: carlos8f commented@fgm LOL you're STILL HERE? I quit Drupal so many years ago. Good Riddance. I use Node.js now, it's so clean, fast, and far superior in every way to PHP. For realz. https://s8f.org/
Comment #15
carlos8f CreditAttribution: carlos8f commentedi helped build Drupal 6/7 with @chx, @webchick, @catch, @drothstein, @sun, @dries, @Damien Tournoud, @Dave Reid, @Fabianx, etc.
That was fun, I made some friends with real-life hackers at DrupalCon SF (a long time ago...). PHP sucks though. Seriously. Use Node.js or Python.
Comment #16
fgm@carlos8f : doing a lot of Node in the last 2 years and hated every minute of it. :troll: I very much prefer working with Go or PHP, but YMMV.
Anyway, rerolled the #6 patch by thebuckst0p on current 7.x-1.x HEAD.
Comment #17
fgmRerolled the #9 patch by khayong on top of today's HEAD.
For ease of use, this issue is available as a branch on the github repo as a pull request: https://github.com/fgm/mongodb/pull/32
Since the patches are all different and not compatible, each of them is reverted after being committed, so be sure to checkout the commit matching what you want to use, and comment here so one version can actually be chosen.