栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

深入PHP操作MongoDB的技术总结

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

深入PHP操作MongoDB的技术总结

复制代码 代码如下:

//
//
//
/
$array=array(‘column_name'=>'col'.rand(100,999),'column_exp'=>'xiaocai');
$result=$collection->insert($array); #简单插入
echo “新记录ID:”.$array['_id']; #MongoDB会返回一个记录标识
var_dump($result); #返回:bool(true)
/
$array=array(‘column_name'=>'col'.rand(100,999),'column_exp'=>'xiaocai2′);
$result=$collection->insert($array,true); #用于等待MongoDB完成操作,以便确定是否成功.(当有大量记录插入时使用该参数会比较有用)
echo “新记录ID:”.$array['_id']; #MongoDB会返回一个记录标识
var_dump($result); #返回:array(3) { ["err"]=> NULL ["n"]=> int(0) ["ok"]=> float(1) }
/
#insert($array,array(‘safe'=>false,'fsync'=>false,'timeout'=>10000))

//
/
$where=array(‘column_name'=>'col123′);
$newdata=array(‘column_exp'=>'GGGGGGG','column_fid'=>444);
$result=$collection->update($where,array(‘$set'=>$newdata)); #$set:让某节点等于给定值,类似的还有$pull $pullAll $pop $inc,在后面慢慢说明用法

/
$where=array(‘column_name'=>'col709′);
$newdata=array(‘column_exp'=>'HHHHHHHHH','column_fid'=>123);
$result=$collection->update($where,$newdata);

/
$where=array(‘column_name'=>'col');
$newdata=array(‘column_exp'=>'multiple','91u'=>684435);
$result=$collection->update($where,array(‘$set'=>$newdata),array(‘multiple'=>true));

/
$where=array('91u'=>684435);
$newdata=array(‘column_exp'=>'edit');
$result=$collection->update($where,array(‘$set'=>$newdata,'$inc'=>array('91u'=>-5)));


$where=array(‘column_name'=>'col685′);
$result=$collection->update($where,array(‘$unset'=>'column_exp'));


//
/
$collection->remove(array(‘column_name'=>'col399′));
//$collection->remove(); #清空集合

$id = new MongoId(“4d638ea1d549a02801000011″);
$collection->remove(array(‘_id'=>(object)$id));

//
/
echo ‘count:'.$collection->count().”
”; #全部
echo ‘count:'.$collection->count(array(‘type'=>'user')).”
”; #可以加上条件
echo ‘count:'.$collection->count(array(‘age'=>array(‘$gt'=>50,'$lte'=>74))).”
”; #大于50小于等于74
echo ‘count:'.$collection->find()->limit(5)->skip(0)->count(true).”
”; #获得实际返回的结果数


$cursor = $collection->find()->snapshot();
foreach ($cursor as $id => $value) {
echo “$id: “; var_dump($value); echo “
”;
}


$cursor = $collection->findOne();


$cursor = $collection->find()->fields(array(“age”=>false,”type”=>false));

$cursor = $collection->find()->fields(array(“user”=>true));


$where=array(‘type'=>array(‘$exists'=>true),'age'=>array(‘$ne'=>0,'$lt'=>50,'$exists'=>true));
$cursor = $collection->find($where);

$cursor = $collection->find()->limit(5)->skip(0);

$cursor = $collection->find()->sort(array(‘age'=>-1,'type'=>1)); ##1表示降序 -1表示升序,参数的先后影响排序顺序

$collection->ensureIndex(array(‘age' => 1,'type'=>-1)); #1表示降序 -1表示升序
$collection->ensureIndex(array(‘age' => 1,'type'=>-1),array(‘background'=>true)); #索引的创建放在后台运行(默认是同步运行)
$collection->ensureIndex(array(‘age' => 1,'type'=>-1),array(‘unique'=>true)); #该索引是唯一的


$cursor = $collection->find();
$array=array();
foreach ($cursor as $id => $value) {
$array[]=$value;
}
//
/
?>

复制代码 代码如下:

//
//
//
/
$array=array(‘column_name'=>'col'.rand(100,999),'column_exp'=>'xiaocai');
$result=$collection->insert($array); #简单插入
echo “新记录ID:”.$array['_id']; #MongoDB会返回一个记录标识
var_dump($result); #返回:bool(true)
/
$array=array(‘column_name'=>'col'.rand(100,999),'column_exp'=>'xiaocai2′);
$result=$collection->insert($array,true); #用于等待MongoDB完成操作,以便确定是否成功.(当有大量记录插入时使用该参数会比较有用)
echo “新记录ID:”.$array['_id']; #MongoDB会返回一个记录标识
var_dump($result); #返回:array(3) { ["err"]=> NULL ["n"]=> int(0) ["ok"]=> float(1) }
/
#insert($array,array(‘safe'=>false,'fsync'=>false,'timeout'=>10000))

//
/
$where=array(‘column_name'=>'col123′);
$newdata=array(‘column_exp'=>'GGGGGGG','column_fid'=>444);
$result=$collection->update($where,array(‘$set'=>$newdata)); #$set:让某节点等于给定值,类似的还有$pull $pullAll $pop $inc,在后面慢慢说明用法

/
$where=array(‘column_name'=>'col709′);
$newdata=array(‘column_exp'=>'HHHHHHHHH','column_fid'=>123);
$result=$collection->update($where,$newdata);

/
$where=array(‘column_name'=>'col');
$newdata=array(‘column_exp'=>'multiple','91u'=>684435);
$result=$collection->update($where,array(‘$set'=>$newdata),array(‘multiple'=>true));

/
$where=array('91u'=>684435);
$newdata=array(‘column_exp'=>'edit');
$result=$collection->update($where,array(‘$set'=>$newdata,'$inc'=>array('91u'=>-5)));


$where=array(‘column_name'=>'col685′);
$result=$collection->update($where,array(‘$unset'=>'column_exp'));


//
/
$collection->remove(array(‘column_name'=>'col399′));
//$collection->remove(); #清空集合

$id = new MongoId(“4d638ea1d549a02801000011″);
$collection->remove(array(‘_id'=>(object)$id));

//
/
echo ‘count:'.$collection->count().”
”; #全部
echo ‘count:'.$collection->count(array(‘type'=>'user')).”
”; #可以加上条件
echo ‘count:'.$collection->count(array(‘age'=>array(‘$gt'=>50,'$lte'=>74))).”
”; #大于50小于等于74
echo ‘count:'.$collection->find()->limit(5)->skip(0)->count(true).”
”; #获得实际返回的结果数


$cursor = $collection->find()->snapshot();
foreach ($cursor as $id => $value) {
echo “$id: “; var_dump($value); echo “
”;
}


$cursor = $collection->findOne();


$cursor = $collection->find()->fields(array(“age”=>false,”type”=>false));

$cursor = $collection->find()->fields(array(“user”=>true));


$where=array(‘type'=>array(‘$exists'=>true),'age'=>array(‘$ne'=>0,'$lt'=>50,'$exists'=>true));
$cursor = $collection->find($where);

$cursor = $collection->find()->limit(5)->skip(0);

$cursor = $collection->find()->sort(array(‘age'=>-1,'type'=>1)); ##1表示降序 -1表示升序,参数的先后影响排序顺序

$collection->ensureIndex(array(‘age' => 1,'type'=>-1)); #1表示降序 -1表示升序
$collection->ensureIndex(array(‘age' => 1,'type'=>-1),array(‘background'=>true)); #索引的创建放在后台运行(默认是同步运行)
$collection->ensureIndex(array(‘age' => 1,'type'=>-1),array(‘unique'=>true)); #该索引是唯一的


$cursor = $collection->find();
$array=array();
foreach ($cursor as $id => $value) {
$array[]=$value;
}
//
/
?>

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/50085.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号