<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 - `limit()`和`skip()`方法 > 原文: [https://beginnersbook.com/2017/09/mongodb-limit-and-skip-method/](https://beginnersbook.com/2017/09/mongodb-limit-and-skip-method/) 在本教程中,我們將學習如何在 MongoDB 查詢中使用`limit()`和`skip()`方法。 ## MongoDB 中的`limit()`方法 此方法限制響應特定查詢而返回的文檔數。 語法: ```js db.collection_name.find().limit(number_of_documents) ``` 讓我們舉一個例子來了解如何使用這種方法。可以說,我有一個集合`studentdata`,其中包含以下文檔: ```js > db.studentdata.find().pretty() { "_id" : ObjectId("59bf63380be1d7770c3982af"), "student_name" : "Steve", "student_id" : 2002, "student_age" : 22 } { "_id" : ObjectId("59bf63500be1d7770c3982b0"), "student_name" : "Carol", "student_id" : 2003, "student_age" : 22 } { "_id" : ObjectId("59bf63650be1d7770c3982b1"), "student_name" : "Tim", "student_id" : 2004, "student_age" : 23 } ``` 讓我們說我想找出所有`ID > 2002`的學生的名單,我會用一個判斷寫一個這樣的查詢: > 要了解在查詢文檔時如何指定條件,請閱讀: [MongoDB 查詢文檔](https://beginnersbook.com/2017/09/mongodb-query-document-using-find-method/) ```js db.studentdata.find({student_id : {$gt:2002}}).pretty() ``` ![MongoDB limit method in Query](https://img.kancloud.cn/bb/38/bb38f8ceb4807603866b658c3e1f7de1_498x279.jpg) **使用`limit()`方法限制結果中的文檔:** 讓我說我不希望所有符合條件的文檔。我只想選擇一些文檔,然后我可以使用`limit()`方法來限制文檔的數量。例如,如果我只想輸出一個文檔,那么我會這樣做: ```js > db.studentdata.find({student_id : {$gt:2002}}).limit(1).pretty() { "_id" : ObjectId("59bf63500be1d7770c3982b0"), "student_name" : "Carol", "student_id" : 2003, "student_age" : 22 } ``` ## MongoDB `skip()`方法 `skip()`方法用于在查詢結果中跳過給定數量的文檔。 要理解`skip()`方法的使用,讓我們采用上面我們看到的相同示例。在上面的例子中,我們可以看到,通過使用`limit(1)`,我們設法只獲得一個文檔,這是第一個與給定條件匹配的文檔。如果您不希望第一個文檔符合您的條件,該怎么辦?例如,我們有兩個文檔,其`student_id`值大于 2002 但是當我們使用`limit(1)`將結果限制為 1 時,我們得到了第一個文檔,為了得到符合此條件的第二個文檔,我們可以使用`skip(1)`,這里將跳過第一份文檔。 **不使用`skip()`:** ```js > db.studentdata.find({student_id : {$gt:2002}}).limit(1).pretty() { "_id" : ObjectId("59bf63500be1d7770c3982b0"), "student_name" : "Carol", "student_id" : 2003, "student_age" : 22 } ``` **使用`skip`:** ```js > db.studentdata.find({student_id : {$gt:2002}}).limit(1).skip(1).pretty() { "_id" : ObjectId("59bf63650be1d7770c3982b1"), "student_name" : "Tim", "student_id" : 2004, "student_age" : 23 } ```
                  <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>

                              哎呀哎呀视频在线观看