Mognodb數(shù)據(jù)庫(kù)連接.
默認(rèn)格式
$m = new Mongo();
//這里采用默認(rèn)連接本機(jī)的27017端口,當(dāng)然也可以連接遠(yuǎn)程主機(jī)如 192.168.0.4:27017,如果端口是27017,端口可以省略。
標(biāo)準(zhǔn)連接
$m = new Mongo("mongodb://${username}:${password}@localhost");
實(shí)例:
$m = new Mongo("mongodb://127.0.0.1:27017/admin:admin");
數(shù)據(jù)庫(kù)的用戶名和密碼都是admin
數(shù)據(jù)庫(kù)操作
插入數(shù)據(jù)
?php
//這里采用默認(rèn)連接本機(jī)的27017端口,當(dāng)然你也可以連接遠(yuǎn)程主機(jī)如192.168.0.4:27017
//如果端口是27017,端口可以省略
$m = new Mongo("mongodb://127.0.0.1:27017/admin:admin");
//選擇comedy數(shù)據(jù)庫(kù),如果以前沒該數(shù)據(jù)庫(kù)會(huì)自動(dòng)創(chuàng)建,也可以用$m->selectDB("comedy");
$db = $m->comedy;
//選擇comedy里面的collection集合,相當(dāng)于RDBMS里面的表,也可以使用
$collection = $db->collection;
$db->selectCollection("collection");
/*********添加一個(gè)元素**************/
$obj = array("title" => "php1", "author" => "Bill Watterson");
//將$obj 添加到$collection 集合中
$collection->insert($obj);
/*********添加另一個(gè)元素**************/
$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為大寫
查詢時(shí),每個(gè)Object插入時(shí)都會(huì)自動(dòng)生成一個(gè)獨(dú)特的_id,它相當(dāng)于RDBMS中的主鍵,用于查詢時(shí)非常方便 (_id每一都不同,很像自動(dòng)增加的id)
?php
$param = array("name" => "joe");
$collection->insert($param);
$joe = $collection->findOne(array("_id" => $param['_id']));
print_R($joe);
$m->close();
返回結(jié)果:Array ( [_id] => MongoId Object ( [$id] => 4fd30e21870da83416000002 ) [name] => joe )
更改字段值
?php
$sign = array("title" => 'php1');
$param = array("title" => 'php1','author'=>'test');
$joe = $collection->update($sign, $param);
刪除一個(gè)數(shù)據(jù)庫(kù)
列出所有可用數(shù)據(jù)庫(kù)
創(chuàng)建一個(gè)MongoDB對(duì)象
?php
$mo = new Mongo();
$db = new MongoDB($mo,'dbname');//通過創(chuàng)建方式獲得一個(gè)MongoDB對(duì)象
刪除當(dāng)前DB
?php
$db = $mo->dbname;
$db->drop();
獲得當(dāng)前數(shù)據(jù)庫(kù)名
?php
$db = $mo->dbname;
$db->_tostring();
選擇想要的collection:
//A:
$mo = new Mongo();
$coll = $mo->dbname->collname;//獲得一個(gè)collection對(duì)象
//B:
$db = $mo->selectDB('dbname');
$coll = $db->collname;
//C:
$db = $mo->dbname;
$coll = $db->collname;
//D:
$db = $mo->dbname;
$coll = $db->selectCollectoin('collname');//獲得一個(gè)collection對(duì)象
插入數(shù)據(jù)(MongoCollection對(duì)象
$coll = $mo->db->foo;
$a = array('a'=>'b');
$options = array('safe'=>true);
$rs =$coll->insert($a,$options);
刪除數(shù)據(jù)庫(kù)中的記錄(MongoCollection對(duì)象)
$coll = $mo->db->coll;
$c = array('a'=>1,'s'=>array('$lt'=>100));
$options = array('safe'=>true);
$rs = $coll->remove($c,$options);
更新數(shù)據(jù)庫(kù)中的記錄(MongoCollection對(duì)象)
$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行
返回一個(gè)游標(biāo)記錄對(duì)象MongoCursor。
針對(duì)游標(biāo)對(duì)象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);
您可能感興趣的文章:- PHP封裝的簡(jiǎn)單連接MongoDB類示例
- PHP使用mongoclient簡(jiǎn)單操作mongodb數(shù)據(jù)庫(kù)示例
- php如何利用pecl安裝mongodb擴(kuò)展詳解
- TP5(thinkPHP5)框架mongodb擴(kuò)展安裝及特殊操作示例
- php操作mongodb封裝類與用法實(shí)例
- PHP操作Mongodb封裝類完整實(shí)例