<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                使用`Mongo`之前,需要裝PHP的`mongo`擴展,訪問 http://pecl.php.net/package/mongodb ,選擇最新的版本即可,然后選擇你的PHP版本對應的擴展。 然后使用`Composer`安裝擴展包: ~~~ composer require topthink/think-mongo=2.0.* ~~~ 接下來,需要修改數據庫配置文件中的相關參數: ~~~ // 數據庫類型 'type' => '\think\mongo\Connection', // 設置查詢類 'query' => '\think\mongo\Query', // 服務器地址 'hostname' => '127.0.0.1', // 集合名 'database' => 'demo', // 用戶名 'username' => '', // 密碼 'password' => '', // 端口 'hostport' => '', ~~~ 默認安裝的`mongodb`是沒有用戶名和密碼的,可以留空。如果你的服務器安裝的mongodb提供了用戶名和密碼認證,請自行修改。`MongoDb`一樣支持分布式設置,設置方法和`Mysql`的分布式設置一致。 ## 關于主鍵 `MongoDb`會自動添加`_id`字段而且作為主鍵,該主鍵數據是一個`MongoDB\BSON\ObjectID`對象實例。 為了方便查詢,系統做了封裝,該主鍵的值可以直接當作字符串使用,因此下面的查詢是有效的: ~~~ // 查詢操作 $user = Db::table('user') ->where('_id','589461c0fc122812b4007411') ->find(); // 或者直接使用 $user = Db::table('user') ->find('589461c0fc122812b4007411'); ~~~ 為了保持和Mysql一致的主鍵命名習慣,系統提供了一個數據庫配置參數`pk_convert_id`可以強制把`_id`轉換為`id`進行操作。 ~~~ // 強制把_id轉換為id 'pk_convert_id' => true, ~~~ 設置后,就可以把`id`當成`_id`來使用 ~~~ // 查詢操作 $user = Db::table('user') ->where('id','589461c0fc122812b4007411') ->find(); dump($user); ~~~ 輸出結果為: ~~~ array (size=3) 'name' => string 'thinkphp' (length=8) 'email' => string 'thinkphp@qq.com' (length=15) 'id' => string '589461c0fc122812b4007411' (length=24) ~~~ 原來的`_id`已經變成`id`,而且是一個字符串類型。 ## 方法變化和差異 除了常規的CURD方法之外,包括`value`、`column`、`setInc`、`setDec`、`setField`、`paginate`等方法仍然被支持,更新的時候也支持`data`、`inc`和`dec`方法,包括聚合查詢方法也都支持。 由于數據庫自身的原因,以下鏈式方法在`MongoDb`中不再支持(或者無效): |不再支持的方法| |---| |view| |join| |alias| |group| |having| |union| |lock| |strict| |sequence| |force| |bind| |partition| 針對了`MongoDb`的特殊性增加了如下鏈式操作方法: |方法|描述| |---|---| |skip|設置skip| |awaitData|設置awaitData| |batchSize|設置batchSize| |exhaust|設置exhaust| |modifiers|設置modifiers| |noCursorTimeout|設置noCursorTimeout| |oplogReplay|設置oplogReplay| |partial|設置partial| |maxTimeMS|設置maxTimeMS| |slaveOk|設置slaveOk| |tailable|設置tailable| |writeConcern|設置writeConcern| 并且`fetchPdo`方法改為`fetchCursor`。 ## Mongo原生查詢 系統提供了幾個基礎查詢方法,僅供熟悉`MongoDb`語法的用戶使用。 >[info] ### query ($collection, $query) **collection**:表示當前查詢的集合 **query**:是一個`\MongoDB\Driver\Query`對象,詳細用法可以參考[官方手冊](http://php.net/manual/zh/mongodb-driver-query.construct.php) 代碼示例如下 ~~~ $filter = [ 'author' => 'bjori', 'views' => [ '$gte' => 100, ], ]; $options = [ /* Only return the following fields in the matching documents */ 'projection' => [ 'title' => 1, 'article' => 1, ], /* Return the documents in descending order of views */ 'sort' => [ 'views' => -1 ], ); $query = new MongoDB\Driver\Query($filter, $options); Db::query('demo.user', $query); ~~~ >[info] ### execute ($collection, $bulk) **collection**:表示當前查詢的集合 **bulk**:是一個`\MongoDB\Driver\BulkWrite`對象,詳細用法可以參考[官方手冊](http://php.net/manual/zh/class.mongodb-driver-bulkwrite.php) >[info] ### command ($command, $dbName) **command**:是一個`\MongoDB\Driver\Command`對象,詳細用法參考[官方手冊](http://php.net/manual/zh/class.mongodb-driver-command.php) **dbName**:當前操作的數據庫名稱,留空表示當前數據庫 除此之外,系統還封裝了一個`cmd`方法可以直接執行字符串格式的`mongo`命令,例如: ~~~ // 列出當前的集合 $collections = Db::cmd('listCollections'); ~~~ 更多的`mongo`命令參考`MongoDb`官方手冊。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看