简体   繁体   English

MongoDB count()在php中是不同的

[英]MongoDB count() distinct in php

I am wondering how to count the number of results of this simple mongodb query, using php. 我想知道如何使用PHP计算这个简单的mongodb查询的结果数。

<?php

//connection to mongodb

  $collection = new MongoCollection($db, 'User');

  $retval = $collection->distinct("age");

    var_dump($retval);
?>

How can i count how many results are into $retval variable? 我如何计算$ retval变量中有多少结果?

Soory , to know how many result are in $retval its simple. Soory ,要知道在$ retval中有多少结果很简单。 Just write this: 写下这个:

echo count($retval);

My solution returns the count of child for one item distincted. 我的解决方案返回一个项目的子计数。 Not the count of simple distinct collection ^^ 不是简单的不同集合的计数^^

For example if you have these data: 例如,如果您有这些数据:

Age:  |  User:
--------------
{ "age":"20", "name":"David" }, 
{ "age":"20", "name":"Goerge" }, 
{ "age":"22", "name":"tiffany" }

My solution return an array and when you each this array such as: 我的解决方案返回一个数组,当你每个这个数组,如:

foreach ($retval as $row) 
       echo($row['age'] . ': ' . $row['count'] . "\n");

Data returned: 返回的数据:

20 : 2
22 : 1

The new Aggregation framework is going to make this easy: http://php.net/manual/en/mongocollection.aggregate.php 新的Aggregation框架将使这一切变得简单: http//php.net/manual/en/mongocollection.aggregate.php

$project = array('$project'=> array("age"=>1)); //pick only the age field
$group = array('$group'=>array("_id"=>'$age')); //group by age field
$count = array('$group'=>array("_id"=>null,"count"=>array('$sum'=>1))); //count distinct    
$result = $collection->aggregate(array($project,$group,$count));
echo $result['result'][0]['count']; 

The PHP documentation for MongoDB describe how count in a getting collection: http://www.php.net/manual/en/mongocollection.count.php MongoDB的PHP文档描述了获取集合的计数: http//www.php.net/manual/en/mongocollection.count.php

For have the count over distinct result, you can create a map-reduce in group collecting such as this example: 为了计算不同的结果,您可以在组收集中创建map-reduce,例如:

<?php
$retval = $collection->group(
    array('age' => true), // field(s) to group by
    array('count' => 0), 
    new MongoCode('function(doc, prev) { prev.count += 1 }'), 
    array(null) // condition
    );
?>

The

new MongoCode(..)

function takes the current document and the aggregation to this point and does the aggregation. function将当前文档和聚合带到此点并进行聚合。 And finaly you can get the count: 最后你可以得到数:

print($retval["count"]);
$collection = new MongoCollection($db, 'contacts');  

$keys = array("Department" => 1);

// set intial values
$initial = array("count" => 0);

// JavaScript function to perform
$reduce = "function (obj, prev) { prev.count++; }";


// only use documents where the "state" field is ar

$condition = array('condition' => array("State" => 'AR'));

$results = $collection->group($keys, $initial, $reduce, $condition);

foreach ($results['retval'] as $key => $value) {
    echo 'Contacts in state AR with department '.$value['Department'] . ' are:'. $value['count'];
    echo '<br />';
}

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

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