用PHP实现MONGODB的基本操作

说到php连mongoDB,不得不先介绍一下php的官方手册,网址在:http://us.php.net/manual/en/book.mongo.php

在php的mongo扩展中,提供了4类接口(对象):

1,针对mongoDB连接的操作:Mongo

http://us.php.net/manual/en/class.mongo.php

2,针对mongoDB中数据库的操作:MongoDB

http://us.php.net/manual/en/class.mongodb.php

3,针对mongoDB中collection的操作:MongoCollection

http://us.php.net/manual/en/class.mongocollection.php

4,针对查询结果集的操作:MongoCursor

http://us.php.net/manual/en/class.mongocursor.php

与mongoDB建立连接:

直接实例化mongo类+创建连接:

$mo = new Mongo();//得到一个Mongo连接对象

实例化了一个Mongo类,并且与默认的localhost:27017端口的mongoDB建立连接。

如果想连接到其他的主机,可以这样写:$mongo = new Mongo("mongodb://username:password@192.168.1.22:12345");

另外一种方式,实例化mongo类,再手动建立连接:

$mongo = new Mongo("mongodb://username:password@192.168.1.22:12345",array('connect'=>false));//初始化类

$mongo->connect();//创建连接

Mongo类中有用的一些方法:

Mongo::listDBs()

http://us.php.net/manual/en/mongo.listdbs.php

返回一个包含当前mongo服务上的库(DB)信息的数组。

$mo = new Mongo();

$dbs = $mo->listDBs();//获得一个包含db信息的数组

Mongo::selectCollection($db,$coll)

http://us.php.net/manual/en/mongo.selectcollection.php

返回一个当前连接下的某db中的collection对象。

$mo = new Mongo();

$coll = $mo->selectCollection(’db’,'mycoll’);//得到一个collection对象

选择想要的数据库(Mongo类):

一种方式:

http://us.php.net/manual/en/mongo.get.php

$mongo = new Mongo();

$db = $mongo->foo;//得到一个MongoDB对象

另一种方式:

http://us.php.net/manual/en/mongo.selectdb.php

$mongo = new Mongo();

$db = $mongo->selectDB(’foo’);//得到一个MongoDB对象

MongoDB中有用的函数:

创建一个MongoDB对象

http://us.php.net/manual/en/mongodb.construct.php

$mo = new Mongo();

$db = new MongoDB($mo,’dbname’);//通过创建方式获得一个MongoDB对象

删除当前DB

http://us.php.net/manual/en/mongodb.drop.php

$db = $mo->dbname;

$db->drop();

获得当前数据库名

http://us.php.net/manual/en/mongodb.–tostring.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->selectCollectoin(’collname’);//获得一个collection对象

插入数据(MongoCollection对象):

http://us.php.net/manual/en/mongocollection.insert.php

MongoCollection::insert(array $a,array $options)

array $a 要插入的数组

array $options 选项

safe 是否返回操作结果信息

fsync 是否直接插入到物理硬盘

例程:

$coll = $mo->db->foo;

$a = array(’a'=>’b');

$options = array(’safe’=>true);

$rs =$coll->insert($a,$options);

$rs为一个array型的数组,包含操作信息

删除数据库中的记录(MongoCollection对象):

http://us.php.net/manual/en/mongocollection.remove.php

MongoCollection::remove(array $criteria,array $options)

array $criteria 条件

array $options 选项

safe 是否返回操作结果

fsync 是否是直接影响到物理硬盘

justOne 是否只影响一条记录

例程:

$coll = $mo->db->coll;

$c = array(’a'=>1,’s’=>array(’$lt’=>100));

$options = array(’safe’=>true);

$rs = $coll->remove($c,$options);

$rs为一个array型的数组,包含操作信息

更新数据库中的记录(MongoCollection对象):

http://us.php.net/manual/en/mongocollection.update.php

MongoCollection::update(array $criceria,array $newobj,array $options)

array $criteria 条件

array $newobj 要更新的内容

array $options 选项

safe 是否返回操作结果

fsync 是否是直接影响到物理硬盘

upsert 是否没有匹配数据就添加一条新的

multiple 是否影响所有符合条件的记录,默认只影响一条

例程:

$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);

$rs为一个array型的数组,包含操作信息

查询collection获得单条记录(MongoCollection类):

http://us.php.net/manual/en/mongocollection.findone.php

array MongoCollection::findOne(array $query,array $fields)

array $query 条件

array $fields 要获得的字段

例程:

$coll = $mo->db->coll;

$query = array(’s’=>array(’$lt’=>100));

$fields = array(’a'=>true,’b'=>true);

$rs = $coll->findOne($query,$fields);

如果有结果就返回一个array,如果没有结果就返回NULL

查询collection获得多条记录(MongoCollection类):

http://us.php.net/manual/en/mongocollection.find.php

MongoCursor MongoCollection::find(array $query,array $fields)

array $query 条件

array $fields 要获得的字段

例程:

$coll = $mo->db->coll;

$query = array(’s’=>array(’$lt’=>100));

$fields = array(’a'=>true,’b'=>true);

$cursor = $coll->find($query,$fields);

返回一个游标记录对象MongoCursor。

针对游标对象MongoCursor的操作(MongoCursor类):

http://us.php.net/manual/en/class.mongocursor.php

循环或的结果记录:

$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);

一个血的教训:

http://us.php.net/manual/en/mongocursor.snapshot.php

详见 不断变化的MongDB结果集

在我们做了find()操作,获得$cursor游标之后,这个游标还是动态的,也就是在我获得游标到我循环操作完成对应记录的过程中,默认情况下,这对符合条件的记录如果增加,结果集也会自动增加。换句话说,在我find()之后,到我的游标循环完成这段时间,如果再有符合条件的记录被插入到collection,那么这些记录也会被$cursor获得。

如果你想在获得$cursor之后的结果集不变化,需要这样做:

$cursor = $coll->find($query,$fields);

$cursor->snapshot();

foreache($cursor as $k=>$v){

var_dump($v);

}