How to handle pagination queries properly with mongodb and php?
Posted
by luckytaxi
on Stack Overflow
See other posts from Stack Overflow
or by luckytaxi
Published on 2010-06-09T23:42:28Z
Indexed on
2010/06/10
11:52 UTC
Read the original article
Hit count: 305
Am I doing this right? I went to look at some old PHP code w/ MySQL and I've managed to get it to work, however I'm wondering if there's a much "cleaner" and "faster" way of accomplishing this.
First I would need to get the total number of "documents"
$total_documents = $collection->find(array("tags" => $tag,
"seeking" => $this->session->userdata('gender'),
"gender" => $this->session->userdata('seeking')))->count();
$skip = (int)($docs_per_page * ($page - 1));
$limit = $docs_per_page;
$total_pages = ceil($total_documents / $limit);
// Query to populate array so I can display with pagination
$data['result'] = $collection->find(array("tags" => $tag,
"seeking" => $this->session->userdata('gender'),
"gender" => $this->session->userdata('seeking')))->limit($limit)->skip($skip)->sort(array("_id" => -1));
My question is, can I run the query in one shot? I'm basically running the same query twice, except the second time I'm passing the value to skip between records.
© Stack Overflow or respective owner