zoukankan      html  css  js  c++  java
  • php操作mongodb的常用函数

    连接mongodb:

    $mongoObj = new Mongo("127.0.0.1" , array(
                            'connect'=>true,
                            'persist'=>true
                        ));

    选择库:

    $mongoDB = $mongoObj->selectDB("wxdata");

    选择集合:

    $mongoColletion=$mongoDB->selectCollection("apachelog");

    删除一个集合:

    $mongoColletion->drop();

    插入文档数据:

    $mongoColletion->insert(array("firstname" => "Bob", "lastname" => "Jones" ));

    修改更新文档数据:

    $newdata = array('$set' => array("address" => "1 Smith Lane"));
    $mongoColletion->update(array("firstname" => "Bob"), $newdata);
    
    $mongoColletion->update(
            array("uri" => "/summer_pics"),
            array('$inc' => array("page hits" => 1)),
            array("upsert" => true)
        );
    
    $mongoColletion->update(
            array("name" => "joe"),
            array("username" => "joe312", "createdAt" => new MongoDate()),
            array("upsert" => true)
        );
    
    $today = array('$gt' => new MongoDate(), '$lt' => new MongoDate(strtotime("+1 day")));
    $mongoColletion->update(
            array("birthday" => $today),
            array('$set' => array('gift' => $surprise)),
            array("multiple" => true)
        );

    新增文档数据:

    $obj = array('x' => 1);
    $mongoColletion->save($obj);
    
    $obj['foo'] = 'bar';
    // $obj 不能被再次插入,导致 duplicate _id 错误
    //$mongoColletion->insert($obj);
    $mongoColletion->save($obj);

    移除一个文档数据:

    $mongoColletion->remove(array('httpstatus' => 200), array("justOne" => true));

    统计文档数量:

    $mongoColletion->count(array('httpstatus'=>304));

    查询一个文档:

    $mongoColletion->findOne(array("httpstatus" => 200));

    查看文档索引信息:

    $mongoColletion->getIndexInfo();

    构建文档索引:

    // create an index on 'httpstatus' ascending
    $mongoColletion->ensureIndex(array('httpstatus' => 1));
    // create a unique index on 'size'
    $mongoColletion->ensureIndex(array('size' => 1), array('unique' => true));
    // create a compound index on 'size' ascending and 'status' descending
    $mongoColletion->ensureIndex(array('size' => 1, 'status' => -1));
    
    //全文索引
    $mongoColletion->ensureIndex(
            array(
                'title' => 'text',
                'desc' => 'text',
            ),
            array(
                'name' => 'ExampleTextIndex',
                'weights' => array(
                    'title' => 100,
                    'desc' => 30,
                )
            )
        );

    关闭到mongodb的连接:

    $mongoObj->close();

     从php操作mongodb常用的函数就这么多了。

  • 相关阅读:
    TD课程通的最终版评价
    对学长TD课程通(.apk)的评价
    Angular速查表
    RxJS学习——官网已不推荐使用的方法及替代方案
    Angular学习问题笔记 (2020.6.17)
    技术分享:基本排序算法
    angular4+ionic3 运行报错 Ineffective mark-compacts near heap limit Allocation failed
    Sass混合指令@mixin和@include用法示例
    Git简介、安装和相关配置
    【Angular学习笔记】英雄编辑器
  • 原文地址:https://www.cnblogs.com/yjken/p/3784190.html
Copyright © 2011-2022 走看看