新聞中心

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站建設(shè)、網(wǎng)站制作、麻陽網(wǎng)絡(luò)推廣、微信平臺小程序開發(fā)、麻陽網(wǎng)絡(luò)營銷、麻陽企業(yè)策劃、麻陽品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供麻陽建站搭建服務(wù),24小時服務(wù)熱線:13518219792,官方網(wǎng)址:www.cdcxhl.com
db.collection_name.find().sort({key:1})
其中 key 用來定義要根據(jù)那個字段進(jìn)行排序,后面的值 1 則表示以升序進(jìn)行排序,若要以降序進(jìn)行排序則需要將其設(shè)置為 -1。
【示例】假設(shè)集合“course”中有如下數(shù)據(jù):
> db.course.find()
{ "_id" : ObjectId("60331a7eee79704753940391"), "title" : "HTML教程", "author" : "編程幫", "url" : "http://www.biancheng.com/html/index.html" }
{ "_id" : ObjectId("60331a7eee79704753940392"), "title" : "C#教程", "author" : "編程幫", "url" : "http://www.biancheng.com/csharp/index.html" }
{ "_id" : ObjectId("60331a7eee79704753940393"), "title" : "MongoDB教程", "author" : "編程幫", "url" : "http://www.biancheng.com/mongodb/index.html" }
若要將集合中的數(shù)據(jù)按照 title 字段降序排列,示例代碼如下:
> db.course.find({}, {"title":1,_id:0}).sort({"title":-1})
{ "title" : "MongoDB教程" }
{ "title" : "HTML教程" }
{ "title" : "C#教程" }
注意,如果在使用 sort() 方法時未指定排序的選項,那么 sort() 方法將默認(rèn)按 _id 的升序顯示文檔,如下所示:
> db.course.find({}, {"title":1,_id:0}).sort({})
{ "title" : "HTML教程" }
{ "title" : "C#教程" }
{ "title" : "MongoDB教程" }網(wǎng)站標(biāo)題:MongoDB排序:sort()方法
轉(zhuǎn)載注明:http://www.dlmjj.cn/article/djppedo.html


咨詢
建站咨詢
