我有一个收集用户,它有2个参数(用户名,付费),用户名是一个字符串,付费也是一个字符串,我应该计算参数paid =“true”的用户数
这是我的尝试
<?php
$connection = new MongoClient();
$collection = $connection->Ahmed->user;
$cursor = $collection->find();
var_dump($collection->count());
var_dump($collection->count($cursor->paid=>"true")));
?>
如果有一个mongo的专家可以帮助,谢谢:)
新的驱动程序没有实现$cursor->count()
使用$collection->count()
而不是
$collection->count($filter)
在你的情况下:$filter = array('paid' => 'true')
首先,你需要告诉mongo你想在find()
函数中找到什么。然后它会将光标返回到结果集,您可以在其上调用count()
。
<?php
$connection = new MongoClient();
$collection = $connection->Ahmed->user;
$cursor = $collection->find(array("paid"=>"true"));
var_dump($cursor->count()); //return the total count of documents with "paid" equal to "true"
您可以使用iterator_to_array将响应转换为数组,然后在php中计算数组记录。
<?php
$connection = new MongoClient();
$collection = $connection->Ahmed->user;
$cursor = $collection->find();
var_dump(count(iterator_to_array($cursor)));
?>