PHP操作MongoDB

本文详细介绍了如何使用PHP与MongoDB进行交互,包括记录的插入、更新、删除及查询等基本操作。文中还展示了如何使用PHP实现MongoDB的批量更新、自动累加等功能,并提供了查询计数、设置查询条件等实用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PHP操作MongoDB

原文: http://www.cnblogs.com/jackluo/archive/2013/06/16/3138835.html

//*************************
//
//
//
/
$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));

//*************************
//
/
echocount:’.$collection->count().”
”; #全部 echocount:’.$collection->count(array(‘type’=>’user’)).”
”; #可以加上条件 echocount:’.$collection->count(array(‘age’=>array(‘$gt’=>50,’$lte’=>74))).”
”; #大于50小于等于74 echocount:’.$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; } //************************* // /


什么是
 
    
 
    
 
    

   
posted @ 2016-07-28 17:49 Newman·Li 阅读( ...) 评论( ...) 编辑 收藏
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值