php mongodb group 整理

php 链接mongodb后 可以用group函数

[php]
/*
$keys = array(‘id’ => true);
$initial = array(‘count’=>0);
$reduce = "function (obj, prev) {
//prev.items.push(obj.zrbl);
prev[‘zrbl’] = obj.zrbl;
prev[‘syts’] = obj.syts;
prev[‘count’] += 1;
}";
$cursor = $collection->group($keys, $initial, $reduce);
print_r($cursor[‘retval’]);
*/
[/php]

keys 是group的字段
initial初始字段
reduce 执行的方法

十来万数据的样子,查起来偏慢 , 主要是找了半天没找到怎么order by 然后就改用 aggregate 了

aggregate 同样的结果 返回速度明显提升

[php]
$cond = array(
/*array(
//’$match’ => array(‘_id’ => array(‘$gt’ => 0)),
‘$match’ => array(‘id’ => array(‘$gt’=>0)),
),*/
array(
‘$group’ => array(
‘_id’ => ‘$id’,
‘zrbl’=>array(‘$first’=>’$zrbl’),
‘stime’=>array(‘$first’=>array(‘$dateToString’=>array(‘format’=>’%Y-%m-%d %H:%M:%S’,’date’=>’$eventtime’))),
‘etime’=>array(‘$last’=>array(‘$dateToString’=>array(‘format’=>’%Y-%m-%d %H:%M:%S’,’date’=>’$eventtime’))),
‘count’ => array(‘$sum’ => 1),
),
),
array(
‘$project’=>array(
‘_id’=>1,
‘zrbl’=>1,
‘count’=>1
)
),
array(
‘$sort’ => array("zrbl" => -1),
),
);
$cursor = $collection->aggregate($cond);
print_r($cursor);
[/php]

操作符介绍:
$project:包含、排除、重命名和显示字段
$match:查询,需要同find()一样的参数
$limit:限制结果数量
$skip:忽略结果的数量
$sort:按照给定的字段排序结果
$group:按照给定表达式组合结果
$unwind:分割嵌入数组到自己顶层文件

表达式 描述 实例
$sum 计算总和。 db.mycol.aggregate([{$group : {_id : “$by_user”, num_tutorial : {$sum : “$likes”}}}])
$avg 计算平均值 db.mycol.aggregate([{$group : {_id : “$by_user”, num_tutorial : {$avg : “$likes”}}}])
$min 获取集合中所有文档对应值得最小值。 db.mycol.aggregate([{$group : {_id : “$by_user”, num_tutorial : {$min : “$likes”}}}])
$max 获取集合中所有文档对应值得最大值。 db.mycol.aggregate([{$group : {_id : “$by_user”, num_tutorial : {$max : “$likes”}}}])
$push 在结果文档中插入值到一个数组中。 db.mycol.aggregate([{$group : {_id : “$by_user”, url : {$push: “$url”}}}])
$addToSet 在结果文档中插入值到一个数组中,但不创建副本。 db.mycol.aggregate([{$group : {_id : “$by_user”, url : {$addToSet : “$url”}}}])
$first 根据资源文档的排序获取第一个文档数据。 db.mycol.aggregate([{$group : {_id : “$by_user”, first_url : {$first : “$url”}}}])
$last 根据资源文档的排序获取最后一个文档数据 db.mycol.aggregate([{$group : {_id : “$by_user”, last_url : {$last : “$url”}}}])

注:array(‘$group’ => array(‘_id’ => ‘$id’,’zrbl’=>array(‘$first’=>1),’count’ => array(‘$sum’ => 1),), ), 同一类型操作可以重复进行,按照先后顺序。

 

管道表达式

管道操作符作为“键”,所对应的“值”叫做管道表达式。例如上面例子中{$match:{status:”A”}},$match称为管道操作符,而{status:”A”}称为管道表达式,它可以看作是管道操作符的操作数(Operand),每个管道表达式是一个文档结构,它是由字段名、字段值、和一些表达式操作符组成的,例如上面例子中管道表达式就包含了一个表达式操作符$sum进行累加求和。

每个管道表达式只能作用于处理当前正在处理的文档,而不能进行跨文档的操作。管道表达式对文档的处理都是在内存中进行的。除了能够进行累加计算的管道表达式外,其他的表达式都是无状态的,也就是不会保留上下文的信息。累加性质的表达式操作符通常和$group操作符一起使用,来统计该组内最大值、最小值等,例如上面的例子中我们在$group管道操作符中使用了具有累加的$sum来计算总和。

除了$sum以为,还有以下性质的表达式操作符:

组聚合操作符

Name Description
$addToSet Returns an array of all the unique values for the selected field among for each document in that group.
$first Returns the first value in a group.
$last Returns the last value in a group.
$max Returns the highest value in a group.
$min Returns the lowest value in a group.
$avg Returns an average of all the values in a group.
$push Returns an array of all values for the selected field among for each document in that group.
$sum Returns the sum of all the values in a group.

Bool类型聚合操作符

Name Description
$and Returns true only when all values in its input array are true.
$or Returns true when any value in its input array are true.
$not Returns the boolean value that is the opposite of the input value.

比较类型聚合操作符

Name Description
$cmp Compares two values and returns the result of the comparison as an integer.
$eq Takes two values and returns true if the values are equivalent.
$gt Takes two values and returns true if the first is larger than the second.
$gte Takes two values and returns true if the first is larger than or equal to the second.
$lt Takes two values and returns true if the second value is larger than the first.
$lte Takes two values and returns true if the second value is larger than or equal to the first.
$ne Takes two values and returns true if the values are not equivalent.

算术类型聚合操作符

Name Description
$add Computes the sum of an array of numbers.
$divide Takes two numbers and divides the first number by the second.
$mod Takes two numbers and calcualtes the modulo of the first number divided by the second.
$multiply Computes the product of an array of numbers.
$subtract Takes two numbers and subtracts the second number from the first.

字符串类型聚合操作符

Name Description
$concat Concatenates two strings.
$strcasecmp Compares two strings and returns an integer that reflects the comparison.
$substr Takes a string and returns portion of that string.
$toLower Converts a string to lowercase.
$toUpper Converts a string to uppercase.

 

Date Aggregation Operators

NOTE

For details on specific operator, including syntax and examples, click on the specific operator to go to its reference page.

Name Description
$dayOfYear Returns the day of the year for a date as a number between 1 and 366 (leap year).
$dayOfMonth Returns the day of the month for a date as a number between 1 and 31.
$dayOfWeek Returns the day of the week for a date as a number between 1 (Sunday) and 7 (Saturday).
$year Returns the year for a date as a number (e.g. 2014).
$month Returns the month for a date as a number between 1 (January) and 12 (December).
$week Returns the week number for a date as a number between 0 (the partial week that precedes the first Sunday of the year) and 53 (leap year).
$hour Returns the hour for a date as a number between 0 and 23.
$minute Returns the minute for a date as a number between 0 and 59.
$second Returns the seconds for a date as a number between 0 and 60 (leap seconds).
$millisecond Returns the milliseconds of a date as a number between 0 and 999.
$dateToString Returns the date as a formatted string.

 

条件类型聚合操作符

Name Description
$cond A ternary operator that evaluates one expression, and depending on the result returns the value of one following expressions.
$ifNull Evaluates an expression and returns a value.

One Response so far.

  1. 419824296说道:
    看看您的博客!

LEAVE A COMMENT