新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)小程序教程:SDK數(shù)據(jù)庫Aggregate·文檔排序
Aggregate.sort(object: Object): Aggregate
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
成都創(chuàng)新互聯(lián)專注于企業(yè)成都全網(wǎng)營(yíng)銷、網(wǎng)站重做改版、新樂網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、html5、電子商務(wù)商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為新樂等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
聚合階段。根據(jù)指定的字段,對(duì)輸入的文檔進(jìn)行排序。
參數(shù)
object: Object
返回值
Aggregate
API 說明
形式如下:
sort({
<字段名1>: <排序規(guī)則>,
<字段名2>: <排序規(guī)則>,
})
<排序規(guī)則>可以是以下取值:
- 1 代表升序排列(從小到大);
- -1 代表降序排列(從大到小);
示例
升序/降序排列
假設(shè)我們有集合 articles,其中包含數(shù)據(jù)如下:
{ "_id": "1", "author": "stark", "score": 80, "age": 18 }
{ "_id": "2", "author": "bob", "score": 60, "age": 18 }
{ "_id": "3", "author": "li", "score": 55, "age": 19 }
{ "_id": "4", "author": "jimmy", "score": 60, "age": 22 }
{ "_id": "5", "author": "justan", "score": 95, "age": 33 }
db.collection('articles')
.aggregate()
.sort({
age: -1,
score: -1
})
.end()
上面的代碼在 students 集合中進(jìn)行聚合搜索,并且將結(jié)果排序,首先根據(jù) age 字段降序排列,然后再根據(jù) score 字段進(jìn)行降序排列。
輸出結(jié)果如下:
{ "_id": "5", "author": "justan", "score": 95, "age": 33 }
{ "_id": "4", "author": "jimmy", "score": 60, "age": 22 }
{ "_id": "3", "author": "li", "score": 55, "age": 19 }
{ "_id": "1", "author": "stark", "score": 80, "age": 18 }
{ "_id": "2", "author": "bob", "score": 60, "age": 18 } 網(wǎng)站名稱:創(chuàng)新互聯(lián)小程序教程:SDK數(shù)據(jù)庫Aggregate·文檔排序
文章轉(zhuǎn)載:http://www.5511xx.com/article/cdedepg.html


咨詢
建站咨詢

