数据的保存
include_once ‘mDB.class.php‘;
$m=new mDB();
$m->setDB(‘mydb‘);
// $m->save(‘stu‘,[‘dept‘=>‘财务‘,‘name‘=>‘张三‘,‘age‘=>73]);
// $m->save(‘stu‘,[‘dept‘=>‘计算机‘,‘name‘=>‘张四‘,‘age‘=>53]);
// $m->save(‘stu‘,[‘dept‘=>‘计算机‘,‘name‘=>‘张五‘,‘age‘=>23]);
// $m->save(‘stu‘,[‘dept‘=>‘财务‘,‘name‘=>‘张六‘,‘age‘=>93]);
// $m->save(‘stu‘,[‘dept‘=>‘计算机‘,‘name‘=>‘张七‘,‘age‘=>83]);
// $m->save(‘stu‘,[‘dept‘=>‘法学‘,‘name‘=>‘张八‘,‘age‘=>53]);
查询出所有的专业
$db=$m->getMongoClient();
$d=$db->mydb;
$c=$d->stu;
// echo $c->find([‘dept‘=>‘计算机‘])->count();//返回计算机专业有几人
$currsor=$c->find([],[‘_id‘=>0,‘dept‘=>1]);
foreach ($currsor as $v){
echo $v[‘dept‘].‘<br>‘;
}
消除重复取出各个专业的名字distinct
$db=$m->getMongoClient();
$d=$db->mydb;
$c=$d->stu;
$depts=$c->distinct(‘dept‘);
foreach ($depts as $v){
echo $v.‘<br>‘;
}
统计各个专业的人数
include_once ‘mDB.class.php‘;
$m=new mDB();
$m->setDB(‘mydb‘);
$db=$m->getMongoClient();
$d=$db->mydb;
$c=$d->stu;
$depts=$c->distinct(‘dept‘);
foreach ($depts as $v){
echo $v;
echo ‘专业人数‘.$c->find([‘dept‘=>$v])->count();
echo ‘<hr>‘;
}
分组查询的所有代码
<?php
header(‘content-type:text/html;charset=UTF-8‘);
//无密码直接登录mongodb
$m = new MongoClient(‘mongodb://localhost:27017‘);
$db = $m->selectDB(‘aaa‘);
$c1 = $db->student;
//$c1->insert(array("category" => "计算机", "name" => "张在",‘age‘=>33));
//$c1->insert(array("category" => "计算机", "name" => "李四",‘age‘=>25));
//$c1->insert(array("category" => "英语", "name" => "赵六",‘age‘=>30));
//$c1->insert(array("category" => "英语", "name" => "赵七",‘age‘=>40));
//$c1->insert(array("category" => "会计", "name" => "李勇",‘age‘=>10));
//$keys = array("category" => 1);
//$initial = array("items" => array());
//$reduce = "function (obj, prev) { prev.items.push(obj);}";
//$reduce = "function (obj, prev) { prev.items.push(obj.name);}";
//$reduce = "function (obj, prev) { prev.items.push(obj.name+obj.age);}";
//$g = $c1->group($keys, $initial, $reduce);
//echo json_encode($g[‘retval‘]);
//echo ‘<pre>‘;
//print_r($g);
//统计各专业人数
// $keys = array("category" => 1); //指定分类属性
// $initial = array("count" => 0);
// $reduce = "function (obj, prev) { prev.count++; }";
// $condition = array(‘condition‘ => array());//条件
// $g = $c1->group($keys, $initial, $reduce, $condition);
// var_dump($g);
//统计年龄大于等于25岁的总人数
// $keys=[];
// $initial = [‘c‘=>0];
// $reduce = ‘function(obj,prev){prev.c++;}‘;
// $where = [‘condition‘=>[‘age‘=>[‘$gte‘=>25]]];
// $g = $c1->group($keys,$initial,$reduce,$where);
// var_dump($g);
//求出各专业学生年龄之和 select category,sum(age) from student group by category;
// $keys=[‘category‘=>1];
// $initial = [‘sum‘=>0];
// $reduce = "function(obj,prev){prev[‘sum‘] += obj.age; }";
// $g = $c1->group($keys,$initial,$reduce);
// var_dump($g);
//根据专业,来统计各专业的最大年龄
// $keys=[‘category‘=>1];
// $initial = [‘max‘=>0];
// $reduce = "function(obj,prev){if(obj.age>prev[‘max‘]){prev[‘max‘]=obj.age;} }";
// $g = $c1->group($keys,$initial,$reduce);
// var_dump($g);
//根据专业分组,求出每个专业的人数,平均年龄,年龄总各
$keys=[‘category‘=>1];
$initial = [‘sum‘=>0,‘count‘=>0,‘avg‘=>0];
$reduce = "function(obj,prev){prev[‘sum‘] += obj.age;prev[‘count‘]++;prev[‘avg‘]=prev[‘sum‘]/prev[‘count‘]}";
$g = $c1->group($keys,$initial,$reduce);
var_dump($g);
$m->close();
查询出所有的信息
include_once ‘mDB.class.php‘;
$m=new mDB();
$m->setDB(‘mydb‘);
$db=$m->getMongoClient();
$d=$db->mydb;
$c=$d->stu;
$keys = ["dept" => 1]; //指定分类属性
$initial = array("count" => 0);
$reduce = "function (obj, prev) { prev.count++; }";
$condition = array(‘condition‘ => array());//条件
$g = $c->group($keys, $initial, $reduce, $condition);
echo ‘<pre>‘;
var_dump($g);
统计出所有的信息
$reduce = "function (obj, prev) { prev.count++; }";此句为程序里面的死命令
<?php
include_once ‘mDB.class.php‘;
$m=new mDB();
$m->setDB(‘mydb‘);
$db=$m->getMongoClient();
$d=$db->mydb;
$c=$d->stu;
$keys = ["dept" => 1]; //指定分类属性
$initial = array("count" => 0);
$reduce = "function (obj, prev) { prev.count++; }";
$condition = array(‘condition‘ => array());//条件
$g = $c->group($keys, $initial, $reduce, $condition);
echo ‘<pre>‘;
var_dump($g);
echo ‘总人数‘.$g[‘count‘];
echo ‘<br>总专业‘.$g[‘keys‘].‘个专业<br>‘;
foreach ($g[‘retval‘] as $v){
echo $v[‘dept‘].‘人数‘.$v[‘count‘].‘人‘;
}
"function(obj,prev){
prev[‘sum‘] += obj.age;
prev[‘count‘]++;
prev[‘avg‘]=prev[‘sum‘]/prev[‘count‘];
if(obj.age>prev[‘max‘]){prev[‘max‘]=obj.age;}
if(obj.age<prev[‘min‘]){prev[‘min‘]=obj.age;}
}";
求出平均分,最高分,最低分,各专业的总分
<?php
include_once ‘mDB.class.php‘;
$m=new mDB();
$m->setDB(‘mydb‘);
$db=$m->getMongoClient();
$d=$db->mydb;
$c=$d->stu;
//根据专业分组,求出每个专业的人数,平均年龄,年龄总各
$keys=[‘dept‘=>1];
$initial = [‘sum‘=>0,‘count‘=>0,‘avg‘=>0,‘max‘=>0,‘min‘=>100];
$reduce ="function(obj,prev){
prev[‘sum‘] += obj.age;
prev[‘count‘]++;
prev[‘avg‘]=prev[‘sum‘]/prev[‘count‘];
if(obj.age>prev[‘max‘]){prev[‘max‘]=obj.age;}
if(obj.age<prev[‘min‘]){prev[‘min‘]=obj.age;}
}";
$g = $c->group($keys,$initial,$reduce);
echo ‘<pre>‘;
var_dump($g);
foreach ($g[‘retval‘] as $v){
echo $v[‘dept‘].‘平均分‘.$v[‘avg‘].‘最高分‘.$v[‘max‘].‘最低分‘.$v[‘min‘].‘各专业总分为‘.$v[‘sum‘].‘<br>‘;
}
时间: 2024-10-05 03:02:22