mongodb aggregate sort

相關問題 & 資訊整理

mongodb aggregate sort

db.users.aggregate( [ $sort : age : -1, posts: 1 } } ] ). This operation sorts the documents in the users collection, in descending order according by the age field and then in ascending order according to the value in the posts field. When comparing va,The documents are sorted by count in descending order. The $sortByCount stage has the following prototype form: copy. copied. $sortByCount: <expression> } ... ,When a $sort immediately precedes a $limit in the pipeline, the $sort operation only maintains the top n results as it progresses, where n is the specified limit, and MongoDB only needs to store n items in memory. This optimization still applies when allo, You almost had it... db.test.aggregate( $group: _id: '$postcode', students: $sum: 1}}}, $sort: _id: -1}} );. gives (I added some test data matching your sample): "result" : [ "_id" : 2003, "students" : 3 }, ", You can do this by $unwind ing the updates array, sorting the resulting docs by date , and then $group ing them back together on _id using the sorted order. db.servers.aggregate( $unwind: '$service.apps.updates'}, $sort: 'service.apps.updates, The mongodb aggregation API seems to have changed. Now you would do db.coll.aggregate([ $group: _id: "$a", countA: $sum: 1}, sumC: $sum: "$c"}, } }, $sort:a:1} } ]). Note the Array syntax for the argument to aggregate(). You'd,Your aggregate query is incorrect. You add the sort and limit to the match, but that's now how you do that. You use different pipeline operators: db.friends.aggregate( [ $match: advertiser: "noc3" } }, $sort: createdDate: -1 } }, $limit, The fields you're sorting on are part of the _id so you need to include that in your $sort field names: db.builds.aggregate([ $group: _id: month: $month: "$time" }, day: $dayOfYear: "$time" }, year: $year: "$time" , You need to create a pipeline that creates the extra refCount field to hold the total count. The first pipeline would be $addField as it allows you to add new fields to the document. The sum is made possible with the $add operator. The preceding pipeline

相關軟體 MongoDB 資訊

MongoDB
MongoDB 是一個免費且開放源碼的跨平檯面向文檔的數據庫程序。分類為 NoSQL 數據庫程序,MongoDB 使用類似 JSON 的文檔與模式。它為使用 MongoDB 包括數據庫開發人員和 DBA 的任何人提供了豐富的 GUI 工具。主要功能包括:全功能嵌入 MongoDB Shell,用戶友好的 Map-Reduce 操作編輯器,創建 / 刪除數據庫,管理集合及其索引的能力,用戶友好的 G... MongoDB 軟體介紹

mongodb aggregate sort 相關參考資料
$sort (aggregation) — MongoDB Manual 3.6 - MongoDB Documentation

db.users.aggregate( [ $sort : age : -1, posts: 1 } } ] ). This operation sorts the documents in the users collection, in descending order according by the age field and then in ascending order accor...

https://docs.mongodb.com

$sortByCount (aggregation) — MongoDB Manual 3.6

The documents are sorted by count in descending order. The $sortByCount stage has the following prototype form: copy. copied. $sortByCount: &lt;expression&gt; }&nbsp;...

https://docs.mongodb.com

$limit (aggregation) — MongoDB Manual 3.6 - MongoDB Documentation

When a $sort immediately precedes a $limit in the pipeline, the $sort operation only maintains the top n results as it progresses, where n is the specified limit, and MongoDB only needs to store n ite...

https://docs.mongodb.com

mongodb aggregation sort - Stack Overflow

You almost had it... db.test.aggregate( $group: _id: &#39;$postcode&#39;, students: $sum: 1}}}, $sort: _id: -1}} );. gives (I added some test data matching your sample): &quot;result&quot; : [ &quo...

https://stackoverflow.com

aggregation framework - Mongodb sort inner array - Stack Overflow

You can do this by $unwind ing the updates array, sorting the resulting docs by date , and then $group ing them back together on _id using the sorted order. db.servers.aggregate( $unwind: &#39;$servi...

https://stackoverflow.com

Mongodb group and sort - Stack Overflow

The mongodb aggregation API seems to have changed. Now you would do db.coll.aggregate([ $group: _id: &quot;$a&quot;, countA: $sum: 1}, sumC: $sum: &quot;$c&quot;}, } }, $sort:a:1} } ]). Note the ...

https://stackoverflow.com

aggregation framework - sort by date with aggregate request in ...

Your aggregate query is incorrect. You add the sort and limit to the match, but that&#39;s now how you do that. You use different pipeline operators: db.friends.aggregate( [ $match: advertiser: &quo...

https://stackoverflow.com

sorting - MongoDB aggregation sort not working - Stack Overflow

The fields you&#39;re sorting on are part of the _id so you need to include that in your $sort field names: db.builds.aggregate([ $group: _id: month: $month: &quot;$time&quot; }, day: $dayOfYear...

https://stackoverflow.com

MongoDb aggregation query using $project and $sort - Stack Overflow

You need to create a pipeline that creates the extra refCount field to hold the total count. The first pipeline would be $addField as it allows you to add new fields to the document. The sum is made ...

https://stackoverflow.com