<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # MongoDB [TOC=2,3] ThinkJS 支持使用 MongoDB 數據庫,底層使用?[mongodb](https://www.npmjs.com/package/mongodb)?模塊。 ## 配置 使用 MongoDB 數據庫,需要將模型中的配置?`type`?改為?`mongo`,修改配置文件?`src/common/config/db.js`: ~~~ export default { type: "mongo" } ~~~ 如果要在連接 MongoDB 服務的時候添加額外的參數,可以通過在?`options`?里追加,如: ~~~ export default { type: "mongo", options: { authSource: "admin" } } ~~~ 上面的配置后,連接 MongoDB 的 URL 變成類似于?`mongodb://127.0.0.1:27017/?authSource=admin`。 更多額外的配置請見?[http://mongodb.github.io/node-mongodb-native/2.0/reference/connecting/connection-settings/](http://mongodb.github.io/node-mongodb-native/2.0/reference/connecting/connection-settings/)。 ## 創建模型 可以通過命令?`thinkjs model [name] --mongo`?來創建模型,如: ~~~ thinkjs model user --mongo ~~~ 執行完成后,會創建文件?`src/common/model/user.js`。如果想創建在其他模塊下,需要帶上具體的模塊名。如: ~~~ thinkjs model home/user --mongo ~~~ 會在?`home`?模塊下創建模型文件,文件為?`src/home/model/user.js`。 ## 模型繼承 模型需要繼承?`think.model.mongo`?類,如果當前類不是繼承該類,需要手動修改。 #### ES6 語法 ~~~ export default class extends think.model.mongo { } ~~~ #### 動態創建類的方式 ~~~ module.exports = think.model("mongo", { }) ~~~ ## CURD 操作 CURD 操作和 Mysql 中接口相同,具體請見?[模型 -> 介紹](https://thinkjs.org/zh-CN/doc/2.0/model_intro.html#toc-d84)。 ## 創建索引 mongo 模型可以配置索引,在增刪改查操作之前模型會自動去創建索引,配置放在?`indexes`?屬性里。如: ~~~ export default class extends think.model.mongo { init(...args){ super.init(...args); //配置索引 this.indexes = { } } } ~~~ ### 單字段索引 ~~~ export default class extends think.model.mongo { init(...args){ super.init(...args); //配置索引 this.indexes = { name: 1 } } } ~~~ ### 唯一索引 通過?`$unique`?來指定為唯一索引,如: ~~~ export default class extends think.model.mongo { init(...args){ super.init(...args); //配置索引 this.indexes = { name: {$unique: 1} } } } ~~~ ### 多字段索引 可以將多個字段聯合索引,如: ~~~ export default class extends think.model.mongo { init(...args){ super.init(...args); //配置索引 this.indexes = { email: 1 test: { name: 1, title: 1, $unique: 1 } } } } ~~~ ## 獲取索引 可以通過方法?`getIndexes`?獲取創建的索引。如: ~~~ export default class extends think.controller.base { async indexAction(){ let model = this.model("user"); let indexes = await model.getIndexes(); } } ~~~ ## aggregate 可以通過?`aggregate`?方法進行混合操作。如: ~~~ export default class extends think.model.mongo { match(){ return this.aggregate([ {$match: {status: "A"}}, {$group: {_id: "$cust_id", total: {$sum: "$amount"}}} ]); } } ~~~ 具體請見?[https://docs.mongodb.org/manual/core/aggregation-introduction/](https://docs.mongodb.org/manual/core/aggregation-introduction/)。 ## MapReduce 可以通過?`mapReduce`?方法進行 MapReduce 操作。如: ~~~ export default class extends think.model.mongo { execMapReduce(){ let map = () => { emit(this.cust_id, this.amount); } let reduce = (key, values) => { return Array.sum(values); } return this.mapReduce(map, reduce, { query: {status: "A"}, out: "order_totals" }) } } ~~~ 具體請見?[https://docs.mongodb.org/manual/core/aggregation-introduction/#map-reduce](https://docs.mongodb.org/manual/core/aggregation-introduction/#map-reduce)。
                  <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>

                              哎呀哎呀视频在线观看