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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # MongoDB 投影 > 原文: [https://beginnersbook.com/2017/09/mongodb-projection/](https://beginnersbook.com/2017/09/mongodb-projection/) 在之前的教程中,我們學習了[如何在查詢文檔](https://beginnersbook.com/2017/09/mongodb-query-document-using-find-method/)時使用條件來從集合中獲取所選文檔。在本教程中,我們將學習 MongoDB 的另一個有趣話題,即 **MongoDB 投影**。當我們想要獲取文檔的選定字段而不是所有字段時使用。 例如,我們有一個集合,其中存儲了包含以下字段的文檔:`student_name`,`student_id`,`student_age`但我們只想查看所有學生的`student_id`,那么在這種情況下我們可以使用投影來獲取`student_id`。 **語法:** ```js db.collection_name.find({},{field_key:1 or 0}) ``` 現在不要擔心語法,我們將看到一個例子來理解這一點。 ## MongoDB 投影示例 讓我們舉一個例子來理解 MongoDB 中的投影。我們有一個名為`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,我們將使用以下投影: ```js > db.studentdata.find({}, {"_id": 0, "student_id": 1}) { "student_id" : 2002 } { "student_id" : 2003 } { "student_id" : 2004 } ``` ![MongoDB Projection Example](https://img.kancloud.cn/9d/02/9d025c0f7b4ad259ae05666ab617481c_494x146.jpg) > 值 1 表示顯示字段,0 表示不顯示該字段。當我們在投影中將字段設置為 1 時,除了`_id`之外,其他字段會自動設置為 0,因此為了避免`_id`,我們需要在投影中將其專門設置為 0。反之亦然,當我們將少數字段設置為 0,其他字段自動設置為 1 時(參見下面的示例) 另一種做同樣事情的方法: ```js > db.studentdata.find({}, {"_id": 0, "student_name": 0, "student_age": 0}) { "student_id" : 2002 } { "student_id" : 2003 } { "student_id" : 2004 } ``` **重要提示:** 有些人在查詢中使用投影時可能會收到此錯誤: ```js Error: error: { "ok" : 0, "errmsg" : "Projection cannot have a mix of inclusion and exclusion.", "code" : 2, "codeName" : "BadValue" } ``` 當您將某些字段設置為 0 而其他字段設置為 1 時會發生這種情況,換句話說,您將混合包含和排除組合在一起,唯一的例外是`_id`字段。例如:以下查詢將產生此錯誤: ```js db.studentdata.find({}, {"_id": 0, "student_name": 0, "student_age": 1}) ``` 這是因為我們將`student_name`設置為 0,將其他字段`student_age`設置為 1。我們不能混用這些。您可以將不想顯示的字段設置為 0,或將字段設置為 1 以顯示。
                  <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>

                              哎呀哎呀视频在线观看