使用PHP计算MongoDB中的速度太慢

I'm trying to check my code, with count lines. But this code works very slow. how can i optimize this code? is there anyway to count?

$find = $conn_stok->distinct("isbn");

for($i=0;$i<=25; $i++) {
    $isbn = $find[$i];

    $countit= $conn_kit->find(array('isbn'=>$isbn))->count();
    if($countit> 0){
        echo "ok<br>";
    } else {
        echo "error<br>";
    }
}

Looks like you are trying to do a simple count(*) group by in the old SQL speak. In MongoDB you would use the aggregation framework to have the database do the work for you instead of doing it in your code.

Here is what the aggregation framework pipeline would look like:

db.collection.aggregate({$group:{_id:"$isbn", count:{$sum:1}}}

I will let you translate that to PHP if you need help there are plenty of examples available.

It looks like you're trying to count the number of 25 top most ISBNs used, and count how often they have been used. In PHP, you would run the following queries. The first one to find all ISBNs, and the second is an aggregation command to do the grouping.

$find = $conn_stok->distinct( 'isbn' );

$aggr = $conn_kit->aggregate(
    // find all ISBNs
    array( '$match' => array( 'isbn' => array( '$in' => $find ) ) ),

    // group those 
    array( '$group' => array( '_id' => '$isbn', count => array( '$sum' => 1 ) ) ),

    // sort by the count
    array( '$sort' => array( 'count' => 1 ) ),

    // limit to the first 25 items (ie, the 25 most used ISBNs)
    array( '$limit' => 25 ),
)

(You're a bit vague as to what $conn_stok and $conn_kit contain and what you want as answer. If you can update your question with that, I can update the answer).