在MongoDB中,選擇字段又叫投影,表示僅選擇所須要字段的數據,而不是選擇整個文檔字段的數據。若是某個文檔有5個字段,但只要顯示3個字段,那麼就只選擇3個字段吧,這樣作是很是有好處的。yii
find()方法在MongoDB查詢文檔中此方法接收的第二個可選參數是要檢索的字段列表。 在MongoDB中,當執行find()方法時,它默認將顯示文檔的全部字段。爲了限制顯示的字段,須要將字段列表對應的值設置爲1或0。1表示顯示字段,而0表示隱藏字段。ide
語法:ui
>db.COLLECTION_NAME.find({},{KEY:1})
mycol有如下數據:url
> db.mycol.find({}, {'_id':1, 'title':1}) { "_id" : 101, "title" : "MongoDB Guide" } { "_id" : 102, "title" : "NoSQL Database" } { "_id" : 104, "title" : "Python Quick Guide" } { "_id" : 100, "title" : "MongoDB Overview" } >
實例:spa
查詢文檔時只顯示文檔的標題。code
> db.mycol.find({}, {'title':1,'_id':0}) { "title" : "MongoDB Guide" } { "title" : "NoSQL Database" } { "title" : "Python Quick Guide" } { "title" : "MongoDB Overview" } > db.mycol.find({}, {'title':1,'by':1, 'url':1}) { "_id" : 101, "title" : "MongoDB Guide", "by" : "yiibai tutorials", "url" : "http://www.yiibai.com" } { "_id" : 102, "title" : "NoSQL Database", "by" : "yiibai tutorials", "url" : "http://www.yiibai.com" } { "_id" : 104, "title" : "Python Quick Guide", "by" : "yiibai tutorials", "url" : "http://www.yiibai.com" } { "_id" : 100, "title" : "MongoDB Overview", "by" : "yiibai tutorials", "url" : "http://www.yiibai.com" } >
注意:在執行find()方法時,始終都會顯示_id字段,若是不想要此字段,則須要將其設置爲0。blog
要限制 MongoDB 中返回的記錄數,須要使用limit()方法。該方法接受一個數字類型參數,它是要顯示的文檔數。排序
語法:ip
> db.COLLECTION_NAME.find().limit(NUMBER)
實例:文檔
mycol有如下數據:
> db.mycol.find({},{'_id':1, 'title':1}) { "_id" : 101, "title" : "MongoDB Guide" } { "_id" : 102, "title" : "NoSQL Database" } { "_id" : 104, "title" : "Python Quick Guide" } { "_id" : 100, "title" : "MongoDB Overview" } >
在查詢文檔時僅顯示兩個文檔。
> db.mycol.find({},{"title":1,_id:0}).limit(2) { "title" : "MongoDB Guide" } { "title" : "NoSQL Database" } >
若是沒有在limit()方法中指定number參數的值,那麼它將顯示集合中的全部文檔。
skip()也能夠接收數字類型參數,用於跳過文檔數量。
語法:
>db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
實例:
僅顯示第三個文檔。
> db.mycol.find({},{"title":1,_id:0}).limit(1).skip(2) { "title" : "Python Quick Guide" } >
注意:skip()方法中的默認值爲0。
要在MongoDB中排序文檔,須要使用sort()方法。該方法接受包含字段列表及其排序順序的文檔。使用指定排序順序1和-1。1用於升序,而-1用於降序。
語法:
>db.COLLECTION_NAME.find().sort({KEY:1})
實例:
mycol有如下數據:
> db.mycol.find({},{'_id':1, 'title':1}) { "_id" : 101, "title" : "MongoDB Guide" } { "_id" : 102, "title" : "NoSQL Database" } { "_id" : 104, "title" : "Python Quick Guide" } { "_id" : 100, "title" : "MongoDB Overview" } >
按標題降序排列顯示文檔。
> ## 按`title`降序排序 > db.mycol.find({},{"title":1,_id:0}).sort({"title":-1}) { "title" : "Python Quick Guide" } { "title" : "NoSQL Database" } { "title" : "MongoDB Overview" } { "title" : "MongoDB Guide" } > ## 按`title`升序排序 > db.mycol.find({},{"title":1,_id:0}).sort({"title":1}) { "title" : "MongoDB Guide" } { "title" : "MongoDB Overview" } { "title" : "NoSQL Database" } { "title" : "Python Quick Guide" } >
按「_id」降序和升序排序顯示文檔。
> 按「_id」升序排序 > db.mycol.find({},{"title":1,_id:1}).sort({"_id":1}) { "_id" : 100, "title" : "MongoDB Overview" } { "_id" : 101, "title" : "MongoDB Guide" } { "_id" : 102, "title" : "NoSQL Database" } { "_id" : 104, "title" : "Python Quick Guide" } > # 按「_id」降序排序 > db.mycol.find({},{"title":1,_id:1}).sort({"_id":-1}) { "_id" : 104, "title" : "Python Quick Guide" } { "_id" : 102, "title" : "NoSQL Database" } { "_id" : 101, "title" : "MongoDB Guide" } { "_id" : 100, "title" : "MongoDB Overview" } >
skip(), limilt(), sort()三個放在一塊兒執行的時候,執行的順序是先 sort(), 而後是 skip(),最後是顯示的 limit()。