Home > php教程 > php手册 > PHP操作mongodb

PHP操作mongodb

WBOY
Release: 2016-06-06 19:42:26
Original
1331 people have browsed it

基础教程: 1.连接mongo数据库服务器。 语法范例: ?php$connection = new MongoClient(); // 连接到本地数据库,默认端口为27017.即:localhost:27017$connection = new MongoClient( mongodb://example.com ); // 连接远程数据库,默认端口为27017$connect

基础教程:

1.连接mongo数据库服务器。

语法范例:

<?php $connection = new MongoClient(); // 连接到本地数据库,默认端口为27017.即:localhost:27017
$connection = new MongoClient( "mongodb://example.com" ); // 连接远程数据库,默认端口为27017
$connection = new MongoClient( "mongodb://example.com:65432" ); // 连接远程数据库,端口号为指定的端口号。
?>
Copy after login

2.连接一个数据库

语法范例:

<?php $connection = new MongoClient();//连接到本地数据库,默认端口:27017
Copy after login
$db = $connection->dbname;//选择数据库名为dbname的数据库
?>
Copy after login
数据库名不用事先创建,当你选择它时,mongodb可以创建一个新的名为dbname数据库。

注意:不要写错数据库名,否则你回不经意间生成一个数据库,这会导致数据库混乱。

以下程序会因拼写错误生成两个数据库:

<?php 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$connection = new MongoClient();
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$db = $connection->mybiloglongdbname;//连接一个数据库
Copy after login
$db = $connection->mybiloglongdbanme;//连接一个不一样名字的数据库
Copy after login
 ?>
Copy after login

3.连接一个数据库集合

获取一个集合和连接一个数据库语法是相同的。

语法范例:

<?php 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$connection = new MongoClient();
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$db = $connection->baz;
$collection = $db->foobar;//选择一个集合
Copy after login
//也可以直接选择一个数据库和一个集合
Copy after login
$collection = $connection->baz->foobar;
Copy after login
?>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
集合和关系型数据库中的表类似。

4.插入一个文档

关联数组是最基本的结构,它保存到集合中。

一些随机的"文档"可以是:

<?php  
Copy after login
Copy after login
$doc = array(
Copy after login
"name"=>"MongoDB",
Copy after login
"type"=>"database",
Copy after login
"count"=>1;
Copy after login
"info"=>(object)array("x"=>203, "y"=>102),
Copy after login
"versions"=>array("0.9.7", "0.9.8", "0.9.9")
Copy after login
);
Copy after login
?>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
注意:你可以有嵌套的数组和对象。驱动程序通常将一个关联数组作为一个数据库中的对像来保存。

一个数字索引数组通常按以下情况作为一个数组来存储:关键字从0开始,不间断。或者作为一个对象

:数组的关键字不是从0开始,中间会有间断。

插入这个文档,可以用这个函数 MongoCollection::insert():

<?php 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$connection = new MongoClient();
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$collection = $connection->database->collectionName;
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$collection->insert($doc);
?>
Copy after login

5,查找一个集合使用 MongoCollection::findOne()

为了能搜索出集合数据,我们事先已经在数据库中存储了相应的数据。我们只需要一个简单的方法 

MongoCollection::findOne() 便可以从集合中取到唯一的文档。这个方法适用于:只有唯一的一条数据匹配

查询数据。

实例:

<?php  
Copy after login
Copy after login
$connection = new MongoClient();
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$collection = $connection->database->collectionName;
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$document = $collection->findOne();
Copy after login
var_dump($document);
Copy after login
?>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
以上实例输出:
array(6) {
["_id"]=>
object(MongoId)#8 (1) {
["$id"]=>
string(24) "4e2995576803fab768000000"
}
["name"]=>
string(7) "MongoDB"
["type"]=>
string(8) "database"
["count"]=>
int(1)
["info"]=>
array(2) {
["x"]=>
    int(203)
    ["y"]=>
    int(102)
  }
  ["versions"]=>
  array(3) {
    [0]=>
    string(5) "0.9.7"
    [1]=>
    string(5) "0.9.8"
    [2]=>
    string(5) "0.9.9"
  }
}
Copy after login
我们注意到这里有一个“_id”的字段已经被自动加到文档中,_id 是主键字段。

如果文档没有定义主键字段,驱动程序会自动添加一个。

若你定义一个你自己的_id字段,它必须对于整个集合是独一无二的。

例子:

<?php 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$connection = new MongoClient();
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$db = $connection->datebase;
Copy after login
$db->foo->insert(array("_id"=>1));
Copy after login
Copy after login
//下面这个将抛出一个异常
Copy after login
$db->foo->insert(array("_id"=>1));
Copy after login
Copy after login
//下面这个是正确的,因为这个是在另外一个集合中
Copy after login
$db->bar->insert(array("_id"=>1));
Copy after login
?>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
你也可以通过传递数组("w"=>0)作为第二个元素,选择关闭该行为。

也就是说驱动程序不必等待数据库去确认写,也不必抛出复制_id的异常。

6.添加多个文档

为了让我们做更多有意思的事情,我们来添加许多简单的文档给集合。

这些文档仅仅是这种形式的数据array("i"=>value),我们可以在一个循环中相当有效率的添加上。

<?php 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$connection = new MongoClient();
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$collection = $connection->database->collectionName;
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
for($i=0;$i<div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">{
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$collection->insert(array('i'=>$i, "field{$i}"=>$i*2));
Copy after login
}
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
?>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
注意:我们可以插入关键字不同的文档在同一个集合中,这也就是为什么我们称MongoDB是模式自由。

7.统计一个集合中文档数目

当我们在集合中插入101个文档时,我们可以用方法MongoCollection::count()来统计文档数:

<?php 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$connection = new MongoClient();
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$collection = $connection->database->collectionName;
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
echo $collection->count();
Copy after login
?>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
输出结果为:101

8.运用Cursor得到所有的文档

为了得到集合中所有的文档,我们将用MongoCollection::find()。

find()方法可以返回一个MongoCursor对象,这个对象允许我们可以迭代出符合我们查询条件的文档。

然后 查询所有文档,并打印出来。

<?php 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$connection = new MongoClient();
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$collection = $connection->database->collectionName;
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$cursor = $collection->find();
Copy after login
foreach($cursor as $id=>$value)
Copy after login
{
Copy after login
Copy after login
Copy after login
Copy after login
echo "$id:";
Copy after login
var_dump($value);
Copy after login
}
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
?>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
可以打印出集合中所有的文档。

$id i是 文档的_id字段,$value是文档本身。

9.设置查询标准

我们可以用一条查询语句通过方法 MongoCollection::find()来获得集合中文档的一个子集。

例如:如果你想找到  "i"字段的值为71的文档,我们可以按下面的做法去做:

<?php 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$connection = new MongoClient();
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$collection = $connection->database->collectionName;
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$query = array('i'=71);
Copy after login
$cursor = $collection->find($query);
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
while($cursor->hasNext)
Copy after login
Copy after login
{
Copy after login
Copy after login
Copy after login
Copy after login
var_dump($cursor->getNext());
Copy after login
Copy after login
Copy after login
}
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
?>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
输出为:
array(2) {
  ["_id"]=>
  object(MongoId)#6 (0) {
  }
  ["i"]=>
  int(71)
  ["_ns"]=>
  "testCollection"
}
Copy after login

10.查询一系列文档

我们可以从集合中查询一系列文档。

例如,如果你想获得‘i’>50的所有文档,我们可以按下面这样去写

<?php 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$connection = new MongoClient();
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$collection = $connection->database->collectionName;
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$query = array("i"=>array('$gt'=>50));
Copy after login
$cursor = $coll->find($query);
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
while($cursor->hasNext())
Copy after login
{
Copy after login
Copy after login
Copy after login
Copy after login
var_dump($cursor->getNext());
Copy after login
Copy after login
Copy after login
}
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
?>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
我们也可以查询一个范围:比如 20<?php
$connection = new MongoClient();
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$collection = $connection->db->collectionName;
Copy after login
Copy after login
Copy after login
Copy after login
$query = array('i'=>array('$gt'=>20,"\$lte"=>30));
Copy after login
$cursor = $collection->find($query);
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
while($cursor->hasNext)
Copy after login
Copy after login
{
Copy after login
Copy after login
Copy after login
Copy after login
var_dump($cursor->getNext());
Copy after login
Copy after login
Copy after login
}
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
?>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$gt 大于; $gte 大于等于 ; $in  范围 ; $it 小于;$lte小于等于;  
<span>find</span><span>(</span> <span>{</span> <span>qty</span><span>:</span> <span>{</span> <span>$in</span><span>:</span> <span>[</span> <span>5</span><span>,</span> <span>15</span> <span>]</span> <span>}</span> <span>}</span> <span>)</span>
Copy after login

$ne 查询所有不等于该元素的数据;

$nin 查询不在范围内的所有数据

另外注意:单引号下$字符不需要转义字符,但是双引号下需要转义字符

11.创建一个索引

MongoDB支持索引,而且索引可以非常容易的加到集合中。

为了创建一个索引,你需要定义一个字段和方向:升序(1)或者降序(-1)。

以下创建了一个升序的索引在‘i’字段上:

<?php 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$connection = new MongoClient();
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
$collection = $collection->database->collectionName();
Copy after login
Copy after login
Copy after login
Copy after login
$collection->ensureIndex(array("i"=>1));//创建一个在‘i’上升序的索引
Copy after login
$collection->ensureIndex(array("i"=>-1, "j"=>1));//创建一个在‘i’上降序,在‘j’上升序的索引。
Copy after login
?>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
当数据增长时,索引就成了数据库良好的读取性能的关键。
如果你对索引读取性能还不熟悉,你可以参考MongoCollection::ensureIndex()MongoCollection::ensureIndex()的相关文档。
Copy after login

参考:http://www.php.net/manual/zh/mongo.tutorial.connecting.php































Related labels:
source:php.cn
Previous article:php上传文件代码 Next article:PHP实现对MongoDB的基础操作
Statement of this Website
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
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template