MongoDB在PHP中的常用操作小结

yipeiwu_com7年前PHP代码库

$mongodb = new Mongo();

//$connection = new Mongo( "$dburl:$port" ); // connect to a remote host (default port)

$mydb = $mongodb->mydb;  //隐性创建数据库mydb

$mydb = $mongodb->selectDB("mydb");  //直接选择已经存在的数据库

$collection = $mydb->mycollect;   //选择所用文集,如果不存在,自动创建

$collection = $db->selectCollection('mydb');   //只选择,不创建

//插入新纪录

$collection->insert(array("name"=>"l4yn3", "age"=>"10", "sex":"unknow"));


//修改记录

$where = array("name"=>"l4yn3");

$update_item = array('$set'=>array("age"=>"15", "sex":"secret"));

$collection->update($where, $update_item);

$options['multiple'] = true; //默认是 false,是否改变匹配的多行

$collection->update($where, $update_item, $options);


//查询记录

$myinfo = $collection->findOne(array("name"=>"l4yn3"));

$myinfo = $collection->findOne(array("name"=>
"l4yn3"), array("age"=>"15"));


//按条件查找:
$query = array("name"=>"l4yn3");
$cursor = $collection->find($query); //在$collectio集合中查找满足$query的文档
while($cursor->hasNext())
{
var_dump($cursor->getNext()); //返回了数组
}


//返回文档记录数量

$collection->count();


//删除一个数据库:
$connection->dropDB("...");

//列出所有可用数据库:
$m->listDBs(); //无返回值
//关闭连接:
$connection->close();

php各种连接mongodb数据库的参数方式

//连接localhost:27017
$conn = new Mongo();
//连接远程主机默认端口
$conn = new Mongo('test.com');
//连接远程主机22011端口
$conn = new Mongo('test.com:22011');
//MongoDB有用户名密码
$conn = new Mongo("mongodb://${username}:${password}@localhost")
//MongoDB有用户名密码并指定数据库blog
$conn = new Mongo("mongodb://${username}:${password}@localhost/blog");
//多个服务器
$conn = new Mongo("mongodb://localhost:27017,localhost:27018");

相关文章

PHP实现算式验证码和汉字验证码实例

在PHP网站开发中,验证码可以有效地保护我们的表单不被恶意提交,但是如果不使用算式验证码或者汉字验证码,仅仅使用简单的字母或者数字验证码,这样的验证码方案真的安全吗? 大家知道简单数字或...

PHP获取文件夹大小函数用法实例

本文实例讲述了PHP获取文件夹大小函数用法。分享给大家供大家参考。具体如下: <?php // 获取文件夹大小 function getDirSize($dir)...

PHP中集成PayPal标准支付的实现方法分享

PHP中集成PayPal标准支付的实现方法分享

PayPal支付功能其实一直在更新文档和接口,这里说的是一个简单的支付功能大概流程如下 1,在网站的结账页面,设置一个提交到PayPal网站的form,里面有一些金额,商品名称,商家收款...

PHP 使用Echarts生成数据统计报表的实现代码

PHP 使用Echarts生成数据统计报表的实现代码

echarts统计,简单示例 先看下效果图 看下代码 HTML页面 为ECharts准备一个Dom,宽高自定义 <div class="panel panel-info"&g...

PHP数据集构建JSON格式及新数组的方法

自己写了个PHP结果集转换成JSON格式的函数,可以直接调用:复制代码 代码如下:function RecordToJson($recordset) { $jstr='['; while...