MongoDB(六):選擇字段、限制記錄數、排序記錄


1. 選擇字段

在MongoDB中,選擇字段又叫投影,表示僅選擇所需要字段的數據,而不是選擇整個文檔字段的數據。如果某個文檔有5個字段,但只要顯示3個字段,那么就只選擇3個字段吧,這樣做是非常有好處的。

find()方法在MongoDB查詢文檔中此方法接收的第二個可選參數是要檢索的字段列表。 在MongoDB中,當執行find()方法時,它默認將顯示文檔的所有字段。為了限制顯示的字段,需要將字段列表對應的值設置為1或0。1表示顯示字段,而0表示隱藏字段。

語法:

>db.COLLECTION_NAME.find({},{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" }
>

實例:

查詢文檔時只顯示文檔的標題。

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

2. 限制記錄數

2.1 limit()方法

要限制 MongoDB 中返回的記錄數,需要使用limit()方法。該方法接受一個數字類型參數,它是要顯示的文檔數。

語法:

> 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參數的值,那么它將顯示集合中的所有文檔。

2.2 skip()方法

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。

3. 排序記錄

要在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()。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM