php对mongodb的扩展(小试牛刀)
人气:0今天外面刮着呼呼的大风,能在一个温暖的小屋写着博客也是北漂的一种幸福。好了废话不多说,今天主要说一下php连接、操作mongodb,如果你没有看上两期的内容,不知道如何安装php对mongodb的扩展的话请您返回去看《
phphttps://m.qb5200.com/连接mongodbhttps://m.qb5200.com/
tryhttps://m.qb5200.com/{https://m.qb5200.com/
$mongohttps://m.qb5200.com/=https://m.qb5200.com/newhttps://m.qb5200.com/Mongo("mongodb://username:password@127.0.0.1:27017https://img.qb5200.com/download-x/db1");https://m.qb5200.com/
}catch(MongoConnectionExceptionhttps://m.qb5200.com/$e)https://m.qb5200.com/{https://m.qb5200.com/
printhttps://m.qb5200.com/$e->getMessage();https://m.qb5200.com/
exit;https://m.qb5200.com/
}https://m.qb5200.com/
选择数据库bloghttps://m.qb5200.com/
$dbhttps://m.qb5200.com/=https://m.qb5200.com/$mongo->blog;https://m.qb5200.com/
关闭数据库https://m.qb5200.com/
$conn->close();https://m.qb5200.com/
选择操作集合https://m.qb5200.com/
$collectionhttps://m.qb5200.com/=https://m.qb5200.com/$db->users;https://m.qb5200.com/
插入数据https://m.qb5200.com/
$userhttps://m.qb5200.com/=https://m.qb5200.com/array('name'https://m.qb5200.com/=>https://m.qb5200.com/'caleng',https://m.qb5200.com/'city'https://m.qb5200.com/=>https://m.qb5200.com/'beijing');https://m.qb5200.com/
$collection->insert($user);https://m.qb5200.com/
修改数据https://m.qb5200.com/
$newdatahttps://m.qb5200.com/=https://m.qb5200.com/array('$set'https://m.qb5200.com/=>https://m.qb5200.com/array("city"https://m.qb5200.com/=>https://m.qb5200.com/"shanghai"));https://m.qb5200.com/
$collection->update(array("name"https://m.qb5200.com/=>https://m.qb5200.com/"caleng"),https://m.qb5200.com/$newdata);https://m.qb5200.com/
删除数据https://m.qb5200.com/
$collection->remove(array('name'=>'caleng'),https://m.qb5200.com/array("justOne"https://m.qb5200.com/=>https://m.qb5200.com/true));https://m.qb5200.com/
查找数据https://m.qb5200.com/
查找一条数据https://m.qb5200.com/
$result=https://m.qb5200.com/$collection->findone(array("name"=>"caleng"));https://m.qb5200.com/
查询一个列表https://m.qb5200.com/
//找出创建时间大于某一时间的数据https://m.qb5200.com/
$starthttps://m.qb5200.com/=https://m.qb5200.com/1;https://m.qb5200.com/
$counditionarray=array("ctime"=>array('$gt'=>1337184000));https://m.qb5200.com/
$list_datahttps://m.qb5200.com/=https://m.qb5200.com/$this->game_handle->find($counditionarray);https://m.qb5200.com/
$totalhttps://m.qb5200.com/=https://m.qb5200.com/$this->game_handle->count($counditionarray);https://m.qb5200.com/
$list_data->limit($count);https://m.qb5200.com///数据结束位置https://m.qb5200.com/
$list_data->skip($start);https://m.qb5200.com///数据开始取的位置https://m.qb5200.com/
var_dump($list_data);https://m.qb5200.com/
in查询https://m.qb5200.com/
$cursorhttps://m.qb5200.com/=https://m.qb5200.com/$collection->find(array(https://m.qb5200.com/
'name'https://m.qb5200.com/=>https://m.qb5200.com/array('$in'https://m.qb5200.com/=>https://m.qb5200.com/array('Joe',https://m.qb5200.com/'Wendy'))https://m.qb5200.com/
));https://m.qb5200.com/
group查询https://m.qb5200.com/
$collection->insert(array("category"https://m.qb5200.com/=>https://m.qb5200.com/"fruit",https://m.qb5200.com/"name"https://m.qb5200.com/=>https://m.qb5200.com/"apple"));https://m.qb5200.com/
$collection->insert(array("category"https://m.qb5200.com/=>https://m.qb5200.com/"fruit",https://m.qb5200.com/"name"https://m.qb5200.com/=>https://m.qb5200.com/"peach"));https://m.qb5200.com/
$collection->insert(array("category"https://m.qb5200.com/=>https://m.qb5200.com/"fruit",https://m.qb5200.com/"name"https://m.qb5200.com/=>https://m.qb5200.com/"banana"));https://m.qb5200.com/
$collection->insert(array("category"https://m.qb5200.com/=>https://m.qb5200.com/"veggie",https://m.qb5200.com/"name"https://m.qb5200.com/=>https://m.qb5200.com/"corn"));https://m.qb5200.com/
$collection->insert(array("category"https://m.qb5200.com/=>https://m.qb5200.com/"veggie",https://m.qb5200.com/"name"https://m.qb5200.com/=>https://m.qb5200.com/"broccoli"));https://m.qb5200.com/
$keyshttps://m.qb5200.com/=https://m.qb5200.com/array("category"https://m.qb5200.com/=>https://m.qb5200.com/1);https://m.qb5200.com/
$initialhttps://m.qb5200.com/=https://m.qb5200.com/array("items"https://m.qb5200.com/=>https://m.qb5200.com/array());https://m.qb5200.com/
$reducehttps://m.qb5200.com/=https://m.qb5200.com/"functionhttps://m.qb5200.com/(obj,https://m.qb5200.com/prev)https://m.qb5200.com/{https://m.qb5200.com/prev.items.push(obj.name);https://m.qb5200.com/}";https://m.qb5200.com/
$ghttps://m.qb5200.com/=https://m.qb5200.com/$collection->group($keys,https://m.qb5200.com/$initial,https://m.qb5200.com/$reduce);https://m.qb5200.com/
echohttps://m.qb5200.com/json_encode($g['retval']);https://m.qb5200.com/
输出结果:https://m.qb5200.com/
[{"category":"fruit","items":["apple","peach","banana"]},{"category":"veggie","items":["corn","broccoli"]}]https://m.qb5200.com/
可以看出得到的结果是一个二维https://m.qb5200.com/数组https://m.qb5200.com/
array(https://m.qb5200.com/
0https://m.qb5200.com/=>https://m.qb5200.com/array("category"https://m.qb5200.com/=>"fruit",https://m.qb5200.com/"items"=>array("apple","peach","banana")),https://m.qb5200.com/
1https://m.qb5200.com/=>https://m.qb5200.com/array("category"https://m.qb5200.com/=>"veggie",https://m.qb5200.com/"items"=>array("corn","broccoli"))https://m.qb5200.com/
)https://m.qb5200.com/
在这里这写了一些简单的操作,如果您想用php更好的作用mongodbhttps://m.qb5200.com/那就看手册吧。
加载全部内容