简体   繁体   English

如何使用mongodb和php正确处理分页查询?

[英]How to handle pagination queries properly with mongodb and php?

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. 我去看了一些旧的PHP代码与MySQL,我已经设法让它工作,但我想知道是否有一个更“清洁”和“更快”的方式来实现这一点。

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. 我基本上运行相同的查询两次,除了第二次我传递值以跳过记录之间。

-- New code ... - 新代码......

Ok, unless someone knows of another way to do this (if it's possible), I'm going to say it's not doable. 好的,除非有人知道另一种方法(如果可能的话),我会说这不可行。 With that said, I changed the way I run my queries through mongodb, which yielded better looking code. 话虽如此,我改变了通过mongodb运行查询的方式,从而产生了更好看的代码。 ;-) I was trying to minimize the trips to the DB, but oh well hopefully this doesn't take a performance hit. ;-)我试图最小化数据库的行程,但哦,希望这不会影响性能。 My other attempt was to count the number of elements in the array, but quickly found out that wouldn't work since the $limit & $skip parameters would give ITS total number of docs. 我的另一个尝试是计算数组中的元素数量,但很快发现这不起作用,因为$ limit和$ skip参数将给出ITS的总文档数。

$skip = (int)($docs_per_page * ($page - 1));
$limit = $docs_per_page;

$query = array("loc" => array('$near' => array('lat' => $latitude, 'lon' => $longitute) ),
        "tags" => $tag, "seeking" => $this->session->userdata('gender'),
        "gender" => $this->session->userdata('seeking'));

$fields = array("username", "zipcode", "tags", "birth_date");
$total_documents = $collection->find($query, array("_id"))->count();
$data['result'] = $collection->find($query, $fields)->limit($limit)->skip($skip);

Since the result of find()->limit()->skip() is a Mongo_Cursor you don't have to execute the actual query twice. 由于find() - > limit() - > skip()的结果是Mongo_Cursor,因此您不必执行两次实际查询。

The following should work as well : 以下应该也可以:

$skip = (int)($docs_per_page * ($page - 1));
$limit = $docs_per_page;

$query = array("loc" => array('$near' => array('lat' => $latitude, 'lon' => $longitute) ),
    "tags" => $tag, "seeking" => $this->session->userdata('gender'),
    "gender" => $this->session->userdata('seeking'));

$fields = array("username", "zipcode", "tags", "birth_date");
$cursor = $collection->find($query, $fields)->limit($limit)->skip($skip);
$total_documents = $cursor->count();
$data['result'] = $cursor;

btw I first misread your question, I thought you didn't know about limit & skip. 顺便说一下我误读了你的问题,我以为你不知道限制和跳过。

Yes you are doing right. 是的,你做得对。 And you can run query in one shot. 并且您可以一次性运行查询。

Here is a paging example: 这是一个分页示例:

function printStudents(pageNumber, nPerPage) {
   print("Page: " + pageNumber);
   db.students.find().skip((pageNumber-1)*nPerPage).limit(nPerPage).forEach( function(student) { print(student.name + "<p>"); } );
}

Reference: Advanced Queries - MongoDB: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-{{skip%28%29}} 参考: 高级查询 - MongoDB:http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-{{skip%28%29}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM