sort

?? [s?:rt]

n. ??, ??, ??; ??

vt.& vi.

vt .????; ????

vi.????;

MongoDB sort() ??? ???

??: MongoDB?? ???? ????? sort() ???? ?????. sort() ???? ????? ?? ??? ??? ??? ? ???, 1? -1? ???? ?? ??? ??? ? ????. ??? 1? ?????? - 1? ???????.

??: ??>db.COLLECTION_NAME.find().sort({KEY:1})

MongoDB sort() ??? ?

col 集合中的數(shù)據(jù)如下:

{ "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP 教程", "description" : "PHP 是一種創(chuàng)建動(dòng)態(tài)交互性站點(diǎn)的強(qiáng)有力的服務(wù)器端腳本語(yǔ)言。", "by" : "php中文網(wǎng)", "url" : "http://miracleart.cn", "tags" : [ "php" ], "likes" : 200 }
{ "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高級(jí)程序設(shè)計(jì)語(yǔ)言。", "by" : "php中文網(wǎng)", "url" : "http://miracleart.cn", "tags" : [ "java" ], "likes" : 150 }
{ "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一個(gè) Nosql 數(shù)據(jù)庫(kù)", "by" : "php中文網(wǎng)", "url" : "http://miracleart.cn", "tags" : [ "mongodb" ], "likes" : 100 }
以下實(shí)例演示了 col 集合中的數(shù)據(jù)按字段 likes 的降序排列:

>db.col.find({},{"title":1,_id:0}).sort({"likes":-1})
{ "title" : "PHP 教程" }
{ "title" : "Java 教程" }
{ "title" : "MongoDB 教程" }
>