Home  >  Article  >  Backend Development  >  How to operate MongoDB with PHP and simple analysis

How to operate MongoDB with PHP and simple analysis

墨辰丷
墨辰丷Original
2018-06-02 10:09:001536browse

This article mainly introduces the simple method of operating MongoDB with PHP, and briefly analyzes the method of installing and operating MongoDB database with PHP. It involves PHP's basic operation skills such as adding, deleting, modifying and checking the MongoDB database. Friends who need it can refer to it

If php operates MongoDB, first download the MongoDB expansion package from the Internet, https://github.com/mongodb/mongo-php-driver/downloads, and select the corresponding expansion package.

This is what I downloaded and then decompressed. VC6 is suitable for apache, VC9 is suitable for IIS, and ts (thread safe) means that PHP runs in the form of a module.

Then put the php_mongo.dll in the ext folder in PHP, then add extension=php_mongo.dll to PHP.INI and restart apache.

The PHP extension MongoDB package is now installed.

For querying some functions of MongoDB, you can query the manual http://us.php.net/manual/en/class.mongocollection.php

<?php
error_reporting(7);
$conn = new Mongo();
$db = $conn->PHPDataBase;
$collection = $db->PHPCollection;
/*-----------------------------
 * 删除
 *-----------------------------
$collection->remove(array("name" => "xixi111"));
*/
/*------------------------------
 * 插入
 *------------------------------
for($i = 0;$i <= 50;$i++) {
  $data = array("name" => "xixi".$i,"email" => "673048143_".$i."@qq.com","age" => $i*1+20);
  $collection->insert($data);
}
*/
/*-------------------------------
 * 查找
 *-------------------------------
$res = $collection->find(array("age" => array(&#39;$gt&#39; => 25,&#39;$lt&#39; => 40)),array("name" => true));
foreach($res as $v) {
  print_r($v);
}
*/
/*-------------------------------
 * 更新
 *-------------------------------
 $collection->update(array("age" =>22),array(&#39;$set&#39; => array("name" => "demoxixi")));
*/
?>

Summary: The above is the entire article Content, I hope it will be helpful to everyone's study.

Related recommendations:

PHP Learning Record Array Function

PHP Implementation to Export Styled Excel Example Sharing

PHP implements the method of forcing the callback type to be specified based on Callable

The above is the detailed content of How to operate MongoDB with PHP and simple analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn