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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # MongoDB 從集合中刪除文檔 > 原文: [https://beginnersbook.com/2017/09/mongodb-delete-document/](https://beginnersbook.com/2017/09/mongodb-delete-document/) 在本教程中,我們將學習如何從集合中刪除文檔。`remove()`方法用于從 MongoDB 中的集合中刪除文檔。 **`remove()`方法的語法:** ```js db.collection_name.remove(delete_criteria) ``` > 要了解如何在 MongoDB 命令中指定條件,請參閱本教程: [MongoDB 查詢文檔](https://beginnersbook.com/2017/09/mongodb-query-document-using-find-method/)。 ## 使用`remove()`方法刪除文檔 假設我的 MongoDB 數據庫中有一個名為`beginnersbookdb`的集合`students`。 `students`集合中的文檔是: ```js > db.students.find().pretty() { "_id" : ObjectId("59bcecc7668dcce02aaa6fed"), "StudentId" : 1001, "StudentName" : "Steve", "age" : 30 } { "_id" : ObjectId("59bcecc7668dcce02aaa6fef"), "StudentId" : 3333, "StudentName" : "Rick", "age" : 35 } ``` 現在我想刪除這個學生 ID 等于 3333 的學生。要做到這一點,我會使用`remove()`方法編寫一個命令,如下所示: ```js db.students.remove({"StudentId": 3333}) ``` 輸出: ```js WriteResult({ "nRemoved" : 1 }) ``` 驗證文檔是否實際刪除。鍵入以下命令: ```js db.students.find().pretty() ``` 它將列出學生集合的所有文檔。 以下是上述所有步驟的屏幕截圖: ![MongoDB Delete Document](https://img.kancloud.cn/18/e0/18e0d1519e041c33aaf9e00383fba95c_509x445.jpg) ## 如何只刪除一個符合條件的文檔? 當集合中存在多個符合條件的文檔時,如果運行`remove`命令,則將刪除所有這些文檔。但是,有一種方法可以將刪除限制為僅一個文檔,這樣即使有更多文檔符合刪除條件,也只會刪除一個文檔。 ```js db.collection_name.remove(delete_criteria, justOne) ``` 這里`justOne`是一個布爾參數,只取 1 和 0,如果你給 1,那么它會將文檔刪除限制為只有 1 個文檔。這是一個可選參數,如上所述,我們使用`remove()`方法而不使用此參數。 例如,我在集合中有以下記錄。 ```js > db.walkingdead.find().pretty() { "_id" : ObjectId("59bf280cb8e797a22c654229"), "name" : "Rick Grimes", "age" : 32, "rname" : "Andrew Lincoln" } { "_id" : ObjectId("59bf2851b8e797a22c65422a"), "name" : "Negan", "age" : 35, "rname" : "Jeffrey Dean Morgan" } { "_id" : ObjectId("59bf28a5b8e797a22c65422b"), "name" : "Daryl Dixon", "age" : 32, "rname" : "Norman Reedus" } ``` 假設我想刪除年齡等于 32 的文檔。此集合中有兩個文檔符合此條件。但是,為了將刪除限制為 1,我們將`justOne`參數設置為`true`。 ```js db.walkingdead.remove({"age": 32}, 1) ``` 輸出:如您所見,只有一個文檔被刪除。 ```js WriteResult({ "nRemoved" : 1 }) ``` ![Deleting only one document MongoDB](https://img.kancloud.cn/9b/f7/9bf773b6b8d59c55267105cd26c151d1_494x305.jpg) ## 刪除所有文檔 如果要從集合中刪除所有文檔但不想刪除集合本身,則可以使用`remove()`方法,如下所示: ```js db.collection_name.remove({}) ``` > 如果您想刪除該集合以及所有文檔,請參閱本指南:[MongoDB 刪除集合](https://beginnersbook.com/2017/09/mongodb-drop-collection/)
                  <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>

                              哎呀哎呀视频在线观看