<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 `sort()`方法 > 原文: [https://beginnersbook.com/2017/09/mongodb-sort-method/](https://beginnersbook.com/2017/09/mongodb-sort-method/) 在本教程中,我們將學習如何使用`sort()`方法對文檔進行排序。 ## 使用`sort()`方法對文檔進行排序 使用`sort()`方法,您可以根據文檔的特定字段按升序或降序對文檔進行排序。 **`sort()`方法的語法:** ```js db.collecttion_name.find().sort({field_key:1 or -1}) ``` 1 表示升序,-1 表示降序。默認值為 1。 **例如**:集合`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 } ``` 假設我想以**降序**顯示所有文檔的`student_id`: > 要僅顯示特定的文檔字段,我使用的是 [MongoDB 投影](https://beginnersbook.com/2017/09/mongodb-projection/) ```js > db.studentdata.find({}, {"student_id": 1, _id:0}).sort({"student_id": -1}) { "student_id" : 2004 } { "student_id" : 2003 } { "student_id" : 2002 } ``` 要以**升序**顯示所有學生的`student_id`字段: ```js > db.studentdata.find({}, {"student_id": 1, _id:0}).sort({"student_id": 1}) { "student_id" : 2002 } { "student_id" : 2003 } { "student_id" : 2004 } ``` **默認值**:默認為升序,所以如果我在`sort()`方法中沒有提供任何值,那么它將按升序對記錄進行排序,如下所示: ```js > db.studentdata.find({}, {"student_id": 1, _id:0}).sort({}) { "student_id" : 2002 } { "student_id" : 2003 } { "student_id" : 2004 } ``` **您還可以根據您不想顯示的字段對文檔進行排序**:例如,您可以根據`student_id`對文檔進行排序,并顯示`student_age`和`student_name`字段。 ![MongoDB Sort Documents](https://img.kancloud.cn/d4/8d/d48dd8f1e0e9d73778a22dc73ec9dc65_512x178.jpg)
                  <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>

                              哎呀哎呀视频在线观看