<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 功能強大 支持多語言、二開方便! 廣告
                在第一章中我們對?`find`?命令做了一個初步的了解。除了?`selectors`?以外?`find`?還有更豐富的功能。我們已經說過,`find`?返回的結果是一個?`cursor`。我們將進一步看看它到底是什么意思。 ## [](https://github.com/geminiyellow/the-little-mongodb-book/blob/master/zh-cn/mongodb.markdown#字段選擇)字段選擇 在開始?`cursors`?的話題之前,你應該知道?`find`?有第二個可選參數,叫做 "projection"。這個參數是我們要檢索或者排除字段的列表。比如,我們可以僅查詢返回獨角獸的名字而不帶別的字段: ~~~ db.unicorns.find({}, {name: 1}); ~~~ 默認的,`_id`?字段總是會返回的。我們可以通過這樣顯式的把它從返回結果中排除?`{name:1, _id: 0}`。 除了?`_id`?字段,你不能把檢索和排除混合使用。仔細想想,這是有道理的。你只能顯式的檢索或者排除某些字段。 ## [](https://github.com/geminiyellow/the-little-mongodb-book/blob/master/zh-cn/mongodb.markdown#排序ordering)排序(Ordering) 到目前位置我已經提到好多次,?`find`?返回的是一個游標,它只有在需要的時候才會執行。但是,你在 shell 中看確實到的是?`find`?被立刻執行了。這只是 shell 的行為。 我們可以通過一個?`find`?的鏈式方法,觀察到?`cursors`?的真正行為。我們來看看?`sort`。我們指定我們希望排序的字段,以 JSON 方式,其中 1 表示升序 -1 表示降序。比如: ~~~ //heaviest unicorns first db.unicorns.find().sort({weight: -1}) //by unicorn name then vampire kills: db.unicorns.find().sort({name: 1, vampires: -1}) ~~~ 就像關系型數據庫那樣,MongoDB 允許對索引進行排序。我們再稍后將詳細討論索引。那,你應該知道的是,MongoDB 對未經索引的字段進行排序是有大小限制的。就是說,如果你試圖對一個非常大的沒有經過索引的結果集進行排序的話,你會得到個異常。有些人認為這是一個缺點。說實話,我是多希望更多的數據庫可以有這種能力去拒絕未經優化的查詢。(我不是把每個 MongoDB 的缺點硬說成優點,但是我已經看夠了那些缺乏優化的數據庫了,我真心希望他們能有一個 strict-mode。) ## [](https://github.com/geminiyellow/the-little-mongodb-book/blob/master/zh-cn/mongodb.markdown#分頁paging)分頁(Paging) 對結果分頁可以通過?`limit`?和?`skip`?游標方法來實現。比如要獲取第二和第三重的獨角獸,我們可以這樣: ~~~ db.unicorns.find() .sort({weight: -1}) .limit(2) .skip(1) ~~~ 通過?`limit`?和?`sort`?的配合,可以在對非索引字段進行排序時避免引起問題。 ## [](https://github.com/geminiyellow/the-little-mongodb-book/blob/master/zh-cn/mongodb.markdown#計數count)計數(Count) shell 中可以直接對一個集合執行?`count`?,像這樣: ~~~ db.unicorns.count({vampires: {$gt: 50}}) ~~~ 實際上,`count`?是一個?`cursor`?的方法,shell 只是簡單的提供了一個快捷方式。以不提供快捷方式的方法來執行的時候需要這樣(在 shell 中同樣可以執行): ~~~ db.unicorns.find({vampires: {$gt: 50}}) .count() ~~~ ## [](https://github.com/geminiyellow/the-little-mongodb-book/blob/master/zh-cn/mongodb.markdown#小結-2)小結 使用?`find`?和?`cursors`?非常簡單。還講了一些我們后面章節會用到的或是非常特殊情況才用的命令,不過不管怎樣,現在,你應該已經非常熟練使用 mongo shell 以及理解 MongoDB 的基本原則了。
                  <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>

                              哎呀哎呀视频在线观看