<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 插入文檔 > 原文: [https://beginnersbook.com/2017/09/mongodb-insert-document/](https://beginnersbook.com/2017/09/mongodb-insert-document/) 在本教程中,我們將了解如何將文檔插入集合中。我們將在多個示例的幫助下理解這一點。 **將文檔插入集合的語法:** ```js db.collection_name.insert() ``` 讓我們舉一個例子來理解這一點。 ## MongoDB 使用`insert()`插入文檔示例 這里我們將文檔插入名為`beginnersbook`的集合中。下面示例中的字段`course`是一個包含多個鍵值對的數組。 ```js db.beginnersbook.insert( { name: "Chaitanya", age: 30, email: "[email?protected]", course: [ { name: "MongoDB", duration: 7 }, { name: "Java", duration: 30 } ] } ) ``` 您應該看到一條成功的寫消息,如下所示: ```js WriteResult({ "nInserted" : 1 }) ``` > 如果集合不存在,則`insert()`方法創建集合,但如果集合存在,則將文檔插入其中 ![MongoDB Insert Document](https://img.kancloud.cn/43/65/4365cc8c659d2529742cb2f4e820e51a_537x253.jpg) **驗證:** 您還可以通過鍵入以下命令驗證文檔是否已成功插入: ```js db.collection_name.find() ``` 在上面的示例中,我們將文檔插入名為`beginnersbook`的集合中,因此命令應為: ```js > db.beginnersbook.find() { "_id" : ObjectId("59bce797668dcce02aaa6fec"), "name" : "Chaitanya", "age" : 30, "email" : "[email?protected]", "course" : [ { "name" : "MongoDB", "duration" : 7 }, { "name" : "Java", "duration" : 30 } ] } ``` ## MongoDB 示例:在集合中插入多個文檔 要在集合中插入多個文檔,我們定義一個文檔數組,稍后我們在數組變量上使用`insert()`方法,如下例所示。這里我們在名為`students`的集合中插入三個文檔。此命令將在`students`集合中插入數據,如果集合不存在,則它將創建集合并插入這些文檔。 ```js var beginners = [ { "StudentId" : 1001, "StudentName" : "Steve", "age": 30 }, { "StudentId" : 1002, "StudentName" : "Negan", "age": 42 }, { "StudentId" : 3333, "StudentName" : "Rick", "age": 35 }, ]; db.students.insert(beginners); ``` 你會看到這個輸出: ```js BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 3, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] }) ``` 如您所見,它在`nInserted`前面顯示數字 3。這意味著此命令已插入 3 個文檔。 ![MongoDB Insert multiple Documents](https://img.kancloud.cn/c7/a3/c7a333748a91acb1f2f5d8d5bd582fed_537x524.jpg) 驗證文檔是否在集合中。運行此命令: ```js db.students.find() ``` > **你知道嗎?** 您可以以 JSON 格式打印輸出數據,以便您可以輕松閱讀。要以 JSON 格式打印數據,請運行命令`db.collection_name.find().forEach(printjson)` 所以在我們的例子中命令是這樣的: ```js db.students.find().forEach(printjson) ``` 在下面的屏幕截圖中,您可以看到差異。首先,我們使用普通的`find()`方法打印文檔,然后使用 JSON 格式打印相同集合的文檔。 JSON 格式的文檔簡潔易讀。 ![Printing documents in JSON format](https://img.kancloud.cn/f0/f0/f0f044552b6d5d850c02821bff620879_537x481.jpg) 我們還可以使用 MongoDB 2.6 中引入的 New Bulk API 插入多個文檔。我們將在 Bulk API 教程中了解到這一點。
                  <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>

                              哎呀哎呀视频在线观看