//MongoDB的配置
$MONGO_SERVER = array(
'host'=>'127.0.0.1',
'port'=>27017,
'dbname'=>'app',
'user'=>'',
'pwd'=>''
);
//主机端口
$host_port = $MONGO_SERVER['host'] . ":" . $MONGO_SERVER['port'];
//实例化Mongo
$mongo = new MongoClient($host_port);
//选择数据库
$db = $mongo->selectDB($MONGO_SERVER['dbname']);
//在当前数据库创建一个集合
$table = $db->createCollection("member");
//选择一个集合
$collection = $db->member;
//插入的数据
$list = array(
array(
"name" => "XiaoMing",
"email" => "jiang@58haha.cn",
"age" => 24,
"city" => "BeiJing",
"work" => "PHP"
),
array(
"name" => "Tom",
"email" => "jiangzunsao@qq.com",
"age" => 25,
"city" => "BeiJing",
"work" => "Android"
),
array(
"name" => "Ye",
"email" => "shao@163.com",
"age" => 21,
"city" => "JiNan",
"work" => "Android"
)
);
//插入数据库
foreach($list as $info){
$collection->insert($info);
}
//获取全部数据
$data = $collection->find();
//循环数据显示
foreach ($data as $document) {
var_dump($document);
}
//删除文档
$collection->remove(array("name"=>"wang","age"=>24), array("justOne" => true));
//更新文档
$collection->update(array("name"=>"Jiang"), array('$set'=>array("age"=>21)));
//更新
$where = array("money"=> 94);
$param = array('$set'=>array('money'=>100));
$opt = array('upsert'=>0,'multiple'=>1);
$res = $collection->update($where,$param,$opt);
// 显示更新后的文档
$cursor = $collection->find();
//查询所有的数量
$cursor = $collection->find()->count();
//查询age=24的数量
$cursor = $collection->find(array('age'=>24))->count();
//查询age<24的数量
$cursor = $collection->find(array('age'=>array('$lt'=>24)))->count();
//查询age<=24的数量
$cursor = $collection->find(array('age'=>array('$lte'=>24)))->count();
//查询age>24的数量
$cursor = $collection->find(array('age'=>array('$gt'=>24)))->count();
//查询age>=24的数量
$cursor = $collection->find(array('age'=>array('$gte'=>24)))->count();
//查询age!=24的数量
$cursor = $collection->find(array('age'=>array('$ne'=>24)))->count();
//查询age>22并且小于25的数量
$cursor = $collection->find(array('age'=>array('$gt'=>22,'$lt'=>25)))->count();
//查询age>=22并且age<=25的数量
$cursor = $collection->find(array('age'=>array('$gte'=>22,'$lte'=>25)))->count();
//查询age=24并且email为jiang@58haha.cn的数量
$cursor = $collection->find(array('age'=>24,'email'=>'jiang1@58haha.cn'))->count();
//查询age=24并且email为jiang@58haha.cn的数据
$cursor = $collection->find(array('age'=>24,'email'=>'jiang@58haha.cn'));
//查询age=24的两条记录
$cursor = $collection->find(array('age'=>24))->limit(5);
//skip()方法来跳过指定数量的数据
$cursor = $collection->find(array('age'=>24))->limit(5)->skip(4);
//sort()方法对数据进行排序,1为升序,-1降序
$cursor = $collection->find()->sort(array('age'=>-1,'name'=>1));
//查询age=24的,只显示name和email
$cursor = $collection->find(array('age'=>24))->fields(array('name'=>1,'email'=>1));
//查询age=24的,不显示work
$cursor = $collection->find(array('age'=>24))->fields(array('work'=>0));
//模糊like查询
$param = array("name" => new MongoRegex('/^W/'));
$cursor = $collection->find($param);
$param = array("name" => new MongoRegex('/ang$/'));
$cursor = $collection->find($param);
$param = array("name" => new MongoRegex('/iang/'));
$cursor = $collection->find($param);
//$or
$param = array('$or'=>array(array("name"=>"Jiang"),array("name"=>"Qiang")));
$cursor = $collection->find($param)->fields(array('name'=>1,'email'=>1));
//$in和$nin
$param = array("age" => array('$in'=>array(21,22,23)));
$cursor = $collection->find($param)->fields(array('name'=>1,'email'=>1));
$param = array("age" => array('$nin'=>array(21,22,23)));
$cursor = $collection->find($param)->fields(array('name'=>1,'email'=>1));
//MongoDB中聚合的方法使用aggregate()都相当于mysql中的group by
// db.user.aggregate([{$group:{_id:"$user",numCount:{avg:1}}}])
// $sum---计算总和
// db.user.aggregate([{$group:{_id:"$user",numCount:{$sum:"$likes"}}}])
// $avg---计算平均值
// db.user.aggregate([{$group:{_id:"$user",numCount:{$avg:"$likes"}}}])
// $min---获取集合中所有文档对应值得最小值
// db.user.aggregate([{$group:{_id:"$user",numCount:{$min:"$likes"}}}])
// $max---获取集合中所有文档对应值得最大值
// db.user.aggregate([{$group:{_id:"$user",numCount:{$max:"$likes"}}}])
$option = array(
//匹配条件
array(
'$match'=>array(
'likes'=>array(
'$gt'=>80,
'$lt'=>110
)
)
),
//统计条件
array(
'$group'=>array(
'_id'=>'$user',
'numcount'=>array(
'$sum'=>'$likes'
)
)
)
);
//获取全部数据
$data = $collection->aggregate($option);
//循环数据显示
$info = array();
foreach ($data as $document) {
$info[] = $document;
}
//分页代码
//设置分页数
$page = isset($_GET['page']) ? max(1,(int)$_GET['page']) : 1;
//设置每页的数量
$pagesize = 3;
//获取总的数据量
$count = $collection->find()->count();
//计算总页数
$totalPage = ceil($count/$pagesize);
if($page>$totalPage){
$page = $totalPage;
}
//开始设置分页的skip
$skip = ($page-1) * $pagesize;
//构造分页字符串
$pageStr = "";
for($i = 1; $i <= $totalPage; $i++){
if($i == $page){
$pageStr .= ' <strong>'.$i.'</strong>';
}else{
$pageStr .= "<a href='?page=$i'>".$i."</a>";
}
}
//获取数据
$allData = $collection->find()->skip($skip)->limit($pagesize);
$data = array();
foreach($allData as $info){
$data[] = $info;
}
PHP操作MongoDB
最新推荐文章于 2024-05-07 22:30:00 发布