zoukankan      html  css  js  c++  java
  • 用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类,再手动建立连接:(http://www.my400800.cn )

    $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

     

     

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

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

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

    $cursor->snapshot();

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

    var_dump($v);

    }




    下面让我们使用group操作,根据group_id分组,汇总计算count:

    <?php

    ini_set('mongo.native_long', 1);

    $instance = new Mongo();

    $instance = $instance->selectCollection('test', 'test');

    $keys = array('group_id' => 1);

    $initial = array('count' => 0);

    $reduce = '
        function(obj, prev) {
            prev.count += obj.count;
        }
    ';

    $result = $instance->group($keys, $initial, $reduce);

    var_dump($result);

    ?>

    结果和预想的有出入,count没有实现累加,而是变成了[object Object],目前,如果必须使用group操作,那么有两种方法可以缓解这个问题:

    ini_set('mongo.native_long', 0);

    $initial = array('count' => (float)0);

    这两种方法都是治标不治本的权宜之计,既然当前PHP驱动里group的实现有问题,那我们就绕开它,用其它的方式实现同样的功能,这个方式就是MapReduce

    <?php

    ini_set('mongo.native_long', 1);

    $instance = new Mongo();

    $instance = $instance->selectDB('test');

    $map = '
        function() {
            emit(this.group_id, this.count);
        }
    ';

    $reduce = '
        function(key, values) {
            var sum = 0;

            for (var index in values) {
                sum += values[index];
            }

            return sum;
        }
    ';

    $result = $instance->command(array(
        'mapreduce' => 'test',
        'map'       => $map,
        'reduce'    => $reduce
    ));

    $result = iterator_to_array($instance->{$result['result']}->find());

    var_dump($result);

    ?>

  • 相关阅读:
    void*指针的由来
    Linux下的ELF文件格式简介
    通过中断门或陷阱门的转移——【只上传好图一张】
    ELF(Executable and Linking Format)
    call Init8259A——痛定思痛,不要数典忘祖了
    orange's习题——第三章【中断和异常】
    静态库
    可执行文件elf的格式
    谈谈我对攻读计算机研究生的看法
    tips for C
  • 原文地址:https://www.cnblogs.com/jishu/p/2177620.html
Copyright © 2011-2022 走看看