php连接mongodb命令,PHP连接MongoDB操作

要使用PHP与MongoDB交互存储数据,需要使用MongoDB PHP驱动程序(http://pecl.php.net/package/mongo)。 从url下载驱动程序下载PHP驱动程序并确保下载的是正确的版本(如在本示例中:Win10 64位下载的版本是:php_mongo-1.6.8-5.6-ts-vc11-x64.zip)。 现在解压缩存档并将php_mongo.dll放入PHP扩展目录(默认为“ext”),并将以下行添加到php.ini文件中 -

extension = php_mongo.dll

然后重新启动 Apache 服务器,查看 phpinfo() 函数的输出结果 -

59e678008919990ecc56eb7477cdf350.png

进行连接并选择数据库

要进行连接,需要指定数据库名称,如果数据库不存在,那么MongoDB会自动创建它。

以下是连接到数据库的代码片段 -

// connect to mongodb

$m = new MongoClient();

echo "Connection to database successfully
";

// select a database

$db = $m->mydb;

echo "Database mydb selected";

?>

执行程序时,会产生以下结果 -

Connection to database successfully

Database mydb selected

创建一个集合

以下是创建集合的代码片段 -

// connect to mongodb

$m = new MongoClient();

echo "Connection to database successfully";

// select a database

$db = $m->mydb;

echo "Database mydb selected";

$collection = $db->createCollection("phpcol");

echo "Collection created succsessfully";

?>

执行程序时,会产生以下结果 -

Connection to database successfully

Database mydb selected

Collection created succsessfully

插入文档

要将文档插入到MongoDB中,请使用insert()方法。

以下是插入文档的代码片段 -

// connect to mongodb

$m = new MongoClient();

echo "Connection to database successfully";

// select a database

$db = $m->mydb;

echo "Database mydb selected";

$collection = $db->phpcol;

echo "Collection selected succsessfully";

$document = array(

"title" => "MongoDB",

"description" => "database",

"likes" => 100,

"url" => "http://www.yiibai.com/mongodb/",

"by", "tutorials point"

);

$collection->insert($document);

echo "Document inserted successfully";

?>

执行程序时,会产生以下结果 -

Connection to database successfully

Database mydb selected

Collection selected succsessfully

Document inserted successfully

查找所有文件

要从集合中选择所有文档,请使用find()方法。

// connect to mongodb

$m = new MongoClient();

echo "Connection to database successfully";

// select a database

$db = $m->mydb;

echo "Database mydb selected";

$collection = $db->phpcol;

echo "Collection selected succsessfully";

$cursor = $collection->find();

// iterate cursor to display title of documents

foreach ($cursor as $document) {

echo $document["title"]. ', URL is=> ' .$document["url"] . "

";

}

?>

以下是选择所有文档的代码片段 -

Connection to database successfully

Database mydb selected

Collection selected succsessfully

MongoDB, URL is=> http://www.yiibai.com/mongodb/

更新文档

要更新文档,需要使用update()方法。

在下面的例子中,将把插入的文档的标题更新为:MongoDB教程 。 以下是更新文档的代码段 -

// connect to mongodb

$m = new MongoClient();

echo "Connection to database successfully";

// select a database

$db = $m->mydb;

echo "Database mydb selected";

$collection = $db->mycol;

echo "Collection selected succsessfully";

// now update the document

$collection->update(array("title"=>"MongoDB"),

array('$set'=>array("title"=>"MongoDB教程")));

echo "Document updated successfully";

// now display the updated document

$cursor = $collection->find();

// iterate cursor to display title of documents

echo "Updated document";

foreach ($cursor as $document) {

echo $document["title"] .', URL => '.$document["url"] . "

";

}

?>

以下是选择所有文档的代码片段 -

Connection to database successfully

Database mydb selected

Collection selected succsessfully

MongoDB, URL is=> http://www.yiibai.com/mongodb/

删除文档

要删除文档,可使用remove()方法。

在下面的示例中,将删除标题为:MongoDB教程 的文档。 以下是删除文档的代码片段 -

// connect to mongodb

$m = new MongoClient();

echo "Connection to database successfully";

// select a database

$db = $m->mydb;

echo "Database mydb selected";

$collection = $db->phpcol;

echo "Collection selected succsessfully";

// now remove the document

$collection->remove(array("title"=>"MongoDB教程"),false);

echo "Documents deleted successfully";

// now display the available documents

$cursor = $collection->find();

foreach ($cursor as $document) {

echo $document["title"] . "

";

}

?>

以下是选择所有文档的代码片段 -

Connection to database successfully

Database mydb selected

Collection selected succsessfully

Documents deleted successfully

在上面的例子中,第二个参数是布尔类型,用于remove()方法的justOne字段。

其余的MongoDB方法findOne(),save(),limit(),skip(),sort()等与上述相同。

由于此篇教程文章是一个入门级的教程文章,只是讲解有关简单入门的操作,有关更高级的内容,请参考:http://docs.mongodb.com/php-library/

¥ 我要打赏

纠错/补充

收藏

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值