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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # MongoDB - 更新集合中的文檔 > 原文: [https://beginnersbook.com/2017/09/mongodb-update-document/](https://beginnersbook.com/2017/09/mongodb-update-document/) 在 MongoDB 中,我們有兩種方法來更新集合中的文檔。1)`update()`方法,2)`save()`方法。雖然這兩種方法都更新了現有文檔,但它們正在不同的場景中使用。當我們需要更新現有文檔的值時使用`update()`方法,而`save()`方法用于將現有文檔替換為已在其中傳遞的文檔。 要更新 MongoDB 中的文檔,我們在命令中提供條件,并更新與該條件匹配的文檔。要了解標準在 MongoDB 命令中的工作方式,請參閱上一個教程: [MongoDB 查詢文檔](https://beginnersbook.com/2017/09/mongodb-query-document-using-find-method/) ## 使用`update()`方法更新文檔 **語法:** ```js db.collection_name.update(criteria, update_data) ``` **示例:** 例如:假設我在數據庫`beginnersbookdb`中有一個名為`got`的集合。`got`里面的文檔是: ```js > db.got.find().pretty() { "_id" : ObjectId("59bd2e73ce524b733f14dd65"), "name" : "Jon Snow", "age" : 32 } { "_id" : ObjectId("59bd2e8bce524b733f14dd66"), "name" : "Khal Drogo", "age" : 36 } { "_id" : ObjectId("59bd2e9fce524b733f14dd67"), "name" : "Sansa Stark", "age" : 20 } { "_id" : ObjectId("59bd2ec5ce524b733f14dd68"), "name" : "Lord Varys", "age" : 42 } ``` 現在假設我想更新名為“Kit Harington”的 Jon Snow 的名字。對此的命令是: ```js db.got.update({"name":"Jon Snow"},{$set:{"name":"Kit Harington"}}) ``` **輸出:** ![MongoDB Update Document](https://img.kancloud.cn/ce/50/ce5088afe93e3276ecea0e3845ce2431_615x456.jpg) 如您所見,該文檔已更新。 > **你知道嗎?** 默認情況下,更新方法更新單個文檔。在上面的示例中,我們只有一個文檔與條件匹配,但是如果有更多文檔,那么也只會更新一個文檔。要啟用 update()方法來更新多個文檔,您必須將此方法的“multi”參數設置為 true,如下所示。 **使用 update()方法更新多個文檔:** ```js db.got.update({"name":"Jon Snow"}, {$set:{"name":"Kit Harington"}},{multi:true}) ``` ## 使用 save()方法更新文檔 **語法:** ```js db.collection_name.save( {_id:ObjectId(), new_document} ) ``` 讓我們采用我們在上面看到的相同的例子。現在我們要將`Kit Harington`的名稱更新為`Jon Snow`。要使用`save()`方法,您應該知道該文檔的唯一`_id`字段。 > 一個非常重要的**注意事項**是當你在使用`save()`方法時不提供`_id`字段時,它調用`insert()`方法并插入傳遞的文檔作為新文檔進入集合 要獲取文檔的`_id`,可以鍵入以下命令: ```js db.got.find().pretty() ``` 這里有一個集合名稱。這種查找唯一`_id`的方法僅在您擁有少量文檔時才有用,否則在大量文檔中滾動和搜索該`_id`是很繁瑣的。 如果您有大量文檔,那么要獲取特定文檔的`_id`,請使用`find()`方法中的條件。例如:要使用`save()`方法獲取要更新的文檔的`_id`,請鍵入以下命令: ```js > db.got.find({"name": "Kit Harington"}).pretty() { "_id" : ObjectId("59bd2e73ce524b733f14dd65"), "name" : "Kit Harington", "age" : 32 } ``` 現在我們知道該文檔的唯一`_id`字段。讓我們使用`save()`方法編寫命令。 ```js > db.got.save({"_id" : ObjectId("59bd2e73ce524b733f14dd65"), "name": "Jon Snow", "age": 30}) > ``` 你應該看到這個輸出: ```js WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) ``` **驗證更新:** ![MongoDB save() method to update document](https://img.kancloud.cn/f6/ff/f6ff228053aa6d116638a6fc4b939339_557x456.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>

                              哎呀哎呀视频在线观看