<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之旅 廣告
                # 查詢(queries) > 原文:[Queries](http://mongoosejs.com/docs/queries.html) ## Queries 可以通過[模型](http://mongoosejs.com/docs/models.html)的幾個靜態輔助方法檢索文檔。 任何涉及指定查詢條件的[模型](http://mongoosejs.com/docs/api.html#model_Model)方法都可以執行兩種方法: 當一個回調函數: - 通過,操作將立即執行,結果傳遞給回調。 - 未通過,查詢的一個實例被返回,它提供了一個特殊的查詢生成器接口。 > 在mongoose 4,一個[查詢](http://mongoosejs.com/docs/api.html#query-js)有一個 `.then()`功能,因此可以被用來作為一個承諾。 當執行一個查詢回調函數,你指定一個JSON文檔作為你的查詢。JSON文檔的語法是MongoDB的shell一樣。 ``` var Person = mongoose.model('Person', yourSchema); // find each person with a last name matching 'Ghost', selecting the `name` and `occupation` fields Person.findOne({ 'name.last': 'Ghost' }, 'name occupation', function (err, person) { if (err) return handleError(err); console.log('%s %s is a %s.', person.name.first, person.name.last, person.occupation) // Space Ghost is a talk show host. }) ``` 在這里,我們看到,查詢立即執行,并將結果傳遞給我們的回調。所有在Mongoose的回調使用模式:`callback(error, result)`。如果執行查詢時發生錯誤,則錯誤參數將包含一個錯誤文檔,結果將是無效的。如果查詢成功,錯誤參數將為空,結果將與查詢的結果填充。 > 在Mongoose中任何一個回調傳遞給一個查詢,回調如下模式`callback(error, results)`。什么樣的結果是取決于操作:為 [findone()](http://mongoosejs.com/docs/api.html#model_Model.findOne) 它是一種單文檔,[find()](http://mongoosejs.com/docs/api.html#model_Model.find)一個文檔列表,[count()](http://mongoosejs.com/docs/api.html#model_Model.count)文檔數量,[update()](http://mongoosejs.com/docs/api.html#model_Model.update) 文件數量等影響,[API文檔模型](http://mongoosejs.com/docs/api.html#model-js)提供更加詳細的關于什么是傳遞給回調函數。 現在讓我們看看在沒有回調時發生了什么: ``` // find each person with a last name matching 'Ghost' var query = Person.findOne({ 'name.last': 'Ghost' }); // selecting the `name` and `occupation` fields query.select('name occupation'); // execute the query at a later time query.exec(function (err, person) { if (err) return handleError(err); console.log('%s %s is a %s.', person.name.first, person.name.last, person.occupation) // Space Ghost is a talk show host. }) ``` 在上面的代碼中,查詢變量是類型[查詢](http://mongoosejs.com/docs/api.html#query-js)。查詢允許您建立一個查詢使用鏈式語法,而不是指定一個JSON對象。下面的2個例子是等價的。 ``` // With a JSON doc Person. find({ occupation: /host/, 'name.last': 'Ghost', age: { $gt: 17, $lt: 66 }, likes: { $in: ['vaporizing', 'talking'] } }). limit(10). sort({ occupation: -1 }). select({ name: 1, occupation: 1 }). exec(callback); // Using query builder Person. find({ occupation: /host/ }). where('name.last').equals('Ghost'). where('age').gt(17).lt(66). where('likes').in(['vaporizing', 'talking']). limit(10). sort('-occupation'). select('name occupation'). exec(callback); ``` 一個完整的查詢輔助功能列表可以在[API文檔](http://mongoosejs.com/docs/api.html#query-js)中找到。 ### 其他文檔的參考資料 There are no joins in MongoDB but sometimes we still want references to documents in other collections. This is where [population](http://mongoosejs.com/docs/populate.html) comes in. Read more about how to include documents from other collections in your query results [here](http://mongoosejs.com/docs/api.html#query_Query-populate). ### Streaming 你可以從MongoDB查詢結果。你需要調用[Query#cursor()](http://mongoosejs.com/docs/api.html#query_Query-stream)函數代替[Query#exec](http://mongoosejs.com/docs/api.html#query_Query-exec)返回一個 [QueryCursor](http://mongoosejs.com/docs/api.html#querycursor-js)實例。 ``` var cursor = Person.find({ occupation: /host/ }).cursor(); cursor.on('data', function(doc) { // Called once for every document }); cursor.on('close', function() { // Called when done }); ``` ### 下一步 既然我們已經掌握了查詢,讓我們看看[驗證](http://mongoosejs.com/docs/validation.html)。
                  <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>

                              哎呀哎呀视频在线观看