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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 模型(models) > 原文:[Models](http://mongoosejs.com/docs/models.htm) > 翻譯:小蝦米(QQ:509129) ## Models 從我們的Schema定義[模型](http://mongoosejs.com/docs/api.html#model-js)的構造函數編譯。實例這些模型代表[文檔](http://mongoosejs.com/docs/documents.html)可以從我們的數據庫中保存和檢索。從數據庫中依靠這些模型來操作所有文檔創建和檢索。 ### 編譯你的第一個模型 ``` var schema = new mongoose.Schema({ name: 'string', size: 'string' }); var Tank = mongoose.model('Tank', schema); ``` 第一個參數是你的模型集合的單數名稱。Mongoose會自動尋找你的模型名稱的復數形式。因此,對于上面的示例,模型`Tank`是用于數據庫中的`tanks`集合的。.model() 功能使得到schema的副本。確信你已經添加了你想要的一切在調用.model()之前! ### 構建文檔 文檔是我們模型的實例。創建它們,并保存到數據庫是很容易的: ``` var Tank = mongoose.model('Tank', yourSchema); var small = new Tank({ size: 'small' }); small.save(function (err) { if (err) return handleError(err); // saved! }) // or Tank.create({ size: 'small' }, function (err, small) { if (err) return handleError(err); // saved! }) ``` 請注意,沒有`tanks` 將創建/刪除,直到連接您的模型使用是打開的。每一個模型都有一個關聯的連接。當你使用`mongoose.model()`。你的模型將要使用默認mongoose連接。 ``` mongoose.connect('localhost', 'gettingstarted'); ``` 如果你創建一個自定義的連接,使用連接的`model()`函數代替。 ``` var connection = mongoose.createConnection('mongodb://localhost:27017/test'); var Tank = connection.model('Tank', yourSchema); ``` ### 查詢 對于Mongoosecha的查找文檔很容易,它支持[豐富](http://www.mongodb.org/display/DOCS/Advanced+Queries)的查詢MongoDB語法。文件可以使用每個模型中使用 [find](http://mongoosejs.com/docs/api.html#model_Model.find),[findById](http://mongoosejs.com/docs/api.html#model_Model.findById),[findOne](http://mongoosejs.com/docs/api.html#model_Model.findOne),或者[where](http://mongoosejs.com/docs/api.html#model_Model.where),靜態方法。 ``` Tank.find({ size: 'small' }).where('createdDate').gt(oneYearAgo).exec(callback); ``` 查看關于如何使用[查詢](http://mongoosejs.com/docs/api.html#query-js)API的詳細信息的[querying](http://mongoosejs.com/docs/queries.html)章節。 ### 刪除 模型有一個靜態刪除方法,可用于移除所有匹配條件的文檔。 ``` Tank.remove({ size: 'large' }, function (err) { if (err) return handleError(err); // removed! }); ``` ### 更新 每個模型都有自己的更新方法,用于修改數據庫中的文檔,不將它們返回到您的應用程序。 詳細看[API](http://mongoosejs.com/docs/api.html#model_Model.update)文檔。 如果你想要更新一個文檔數據庫,并將結果返回給你的應用程序,使用[`findOneAndUpdate`](http://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate)代替。 ### 然而,更多的 API文檔包含了許多額外的方法,像 [count](http://mongoosejs.com/docs/api.html#model_Model.count),[mapReduce](http://mongoosejs.com/docs/api.html#model_Model.mapReduce),[aggregate](http://mongoosejs.com/docs/api.html#model_Model.aggregate),[更多](http://mongoosejs.com/docs/api.html#model_Model.findOneAndRemove)。
                  <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>

                              哎呀哎呀视频在线观看