Heim  >  Artikel  >  Backend-Entwicklung  >  php操作MongoDB基础教程(连接、新增、修改、删除、查询)_PHP教程

php操作MongoDB基础教程(连接、新增、修改、删除、查询)_PHP教程

WBOY
WBOYOriginal
2016-07-13 10:35:12710Durchsuche

复制代码 代码如下:

//连接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");
//选择数据库blog
$db = $conn->blog;
//制定结果集(表名:users)
$collection = $db->users;
//新增
$user = array('name' => 'caleng', 'email' => 'admin#admin.com');
$collection->insert($user);
//修改
$newdata = array('$set' => array("email" => "test@test.com"));
$collection->update(array("name" => "caleng"), $newdata);
//删除
$collection->remove(array('name'=>'caleng'), array("justOne" => true));
//查找
$cursor = $collection->find();
var_dump($cursor);
//查找一条
$user = $collection->findOne(array('name' => 'caleng'), array('email'));
var_dump($user);
//关闭数据库
$conn->close();


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/745824.htmlTechArticle复制代码 代码如下: //连接localhost:27017 $conn = new Mongo(); //连接远程主机默认端口 $conn = new Mongo('test.com'); //连接远程主机22011端口 $conn = new...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn