mongodb+php 使用方法

Mognodb數據庫鏈接

默認格式

$m = new Mongo();
//這裏採用默認鏈接本機的27017端口,固然也能夠鏈接遠程主機如  192.168.0.4:27017,若是端口是27017,端口能夠省略。

標準鏈接

$m = new Mongo("mongodb://${username}:${password}@localhost");

實例:php

$m = new Mongo("mongodb://127.0.0.1:27017/admin:admin");

數據庫的用戶名和密碼都是adminmysql

數據庫操做

插入數據

<?php
//這裏採用默認鏈接本機的27017端口,固然你也能夠鏈接遠程主機如192.168.0.4:27017
//若是端口是27017,端口能夠省略
$m = new Mongo("mongodb://127.0.0.1:27017/admin:admin");
//選擇comedy數據庫,若是之前沒該數據庫會自動建立,也能夠用$m->selectDB("comedy");
$db = $m->comedy;
//選擇comedy裏面的collection集合,至關於RDBMS裏面的表,也可使用
$collection = $db->collection;
$db->selectCollection("collection");
/*********添加一個元素**************/
$obj = array("title" => "php1", "author" => "Bill Watterson");
//將$obj 添加到$collection 集合中
$collection->insert($obj);
/*********添加另外一個元素**************/
$obj = array("title" => "huaibei", "online" => true);
$collection->insert($obj);
//$query = array("title" => "huaibei");
$query = array( "_id" => $obj['_id'] );
$cursor = $collection->find($query);
//遍歷全部集合中的文檔
foreach ($cursor as $obj) {
   echo $obj["title"] . "\n";
   echo $obj["_id"] . "\n";
}
//斷開MongoDB鏈接
$m->close();

帶條件的查詢

mysql: id = 123
mongo: array(‘id’=>123)
mysql: name link ’%bar%’
mongo: array(‘name’ => new MongoRegex(‘/.*bar.*/i’))
mysql: where id > 10
mongo: array(‘id’ => array(‘$gt’ => 10))
mysql: where id >= 10
mongo: array(‘id’ => array(‘$gte’ => 10))
mysql: where id < 10
mongo: array(‘id’ => array(‘$lt’ => 10))
mysql: where id <= 10
mongo: array(‘id’ => array(‘$lte’ => 10))
mysql: where id > 1 and id < 10
mongo: array(‘id’ => array(‘$gt’ => 1,’$lt’ => 10))
mysql: where id <> 10
mongo: array(‘id’ => array(‘$ne’ => 10))
mysql: where id in(123)
mongo: array(‘id’ => array(‘$in’ => array(1,2,3)))
mysql: where id not in(123)
mongo: array(‘id’ => array(‘$nin’ => array(1,2,3)))
mysql: where id = 2 or id = 9
mongo: array(‘id’ => array(‘$or’ => array(array(‘id’=>2),array(‘id’=>9))))
mysql: order by name asc
mongo: array(‘sort’=>array(‘name’=>1))
mysql: order by name desc
mongo: array(‘sort’=>array(‘name’=>-1))
mysql: limit 0,2
mongo: array(‘limit’=>array(‘offset’=>0,’rows’=>2))
mysql: select name,email
mongo: array(‘name’,'email’)
mysql: select count(name)
mongo: array(‘COUNT’) //注意:COUNT爲大寫

查詢時,每一個Object插入時都會自動生成一個獨特的_id,它至關於RDBMS中的主鍵,用於查詢時很是方便 (_id每一都不一樣,很像自動增長的id)sql

<?php
$param = array("name" => "joe");
$collection->insert($param);
$joe = $collection->findOne(array("_id" => $param['_id']));
print_R($joe);
$m->close();

返回結果:Array ( [_id] => MongoId Object ( [$id] => 4fd30e21870da83416000002 ) [name] => joe )mongodb

更改字段值

<?php
$sign = array("title" => 'php1');
$param = array("title" => 'php1','author'=>'test');
$joe = $collection->update($sign, $param);

刪除一個數據庫

$m -> dropDB(「comedy」);

列出全部可用數據庫

$m->listDBs();   //無返回值

建立一個MongoDB對象

<?php
$mo = new Mongo();
$db = new MongoDB($mo,’dbname’);//經過建立方式得到一個MongoDB對象

刪除當前DB

<?php
$db = $mo->dbname;
$db->drop();

得到當前數據庫名

<?php
$db = $mo->dbname;
$db->_tostring();

選擇想要的collection:

//A:
$mo = new Mongo();
$coll = $mo->dbname->collname;//得到一個collection對象
//B:
$db = $mo->selectDB(’dbname’);
$coll = $db->collname;
//C:
$db = $mo->dbname;
$coll = $db->collname;
//D:
$db = $mo->dbname;
$coll = $db->selectCollectoin(’collname’);//得到一個collection對象

插入數據(MongoCollection對象

$coll = $mo->db->foo;
$a = array(’a’=>’b’);
$options = array(’safe’=>true);
$rs  =$coll->insert($a,$options);

刪除數據庫中的記錄(MongoCollection對象)

$coll = $mo->db->coll;
$c = array(’a’=>1,’s’=>array(’$lt’=>100));
$options = array(’safe’=>true);
$rs = $coll->remove($c,$options);

更新數據庫中的記錄(MongoCollection對象)

$coll = $mo->db->coll;
$c = array(’a’=>1,’s’=>array(’$lt’=>100));
$newobj = array(’e’=>’f’,’x’=>’y’);
$options = array(’safe’=>true,’multiple’=>true);
$rs = $coll->remove($c,$newobj,$options);

查詢collection得到單條記錄(MongoCollection類)

$coll = $mo->db->coll;
$query = array(’s’=>array(’$lt’=>100));
$fields = array(’a’=>true,’b’=>true);
$rs = $coll->findOne($query,$fields);

查詢collection得到多條記錄(MongoCollection類)

$coll = $mo->db->coll;
$query = array(’s’=>array(’$lt’=>100));
$fields = array(’a’=>true,’b’=>true);
$cursor = $coll->find($query,$fields);
//排序
$cursor->sort(array(‘字段’=>-1));(-1倒序,1正序)
//跳過部分記錄
$cursor->skip(100);跳過100行
//只顯示部分記錄
$cursor->limit(100);只顯示100行
返回一個遊標記錄對象MongoCursor。

針對遊標對象MongoCursor的操做(MongoCursor類)

$cursor = $coll->find($query,$fields);
while($cursor->hasNext()){
$r = $cursor->getNext();
var_dump($r);
}
//或者
$cursor = $coll->find($query,$fields);
foreache($cursor as $k=>$v){
var_dump($v);
}
//或者
$cursor = $coll->find($query,$fields);
$array= iterator_to_array($cursor);

個人博客文章數據庫

相關文章
相關標籤/搜索