MongoDB shell擷取最大值和最小值,mongodbshell

來源:互聯網
上載者:User

MongoDB shell擷取最大值和最小值,mongodbshell

並沒有發現MongoDB有專用的求最大值的方法,不過可以通過排序和取第一條來代替。

下面的集合資料如下:

{ "_id" : ObjectId("54c39358acace71b1bd20a70"), "epoch_min" : NumberLong(1422030840), "usage_ratio" : 0.035140007734298706 }{ "_id" : ObjectId("54c39358acace71b1bd20a71"), "epoch_min" : NumberLong(1422030900), "usage_ratio" : 0.025494230911135674 }{ "_id" : ObjectId("54c39358acace71b1bd20a72"), "epoch_min" : NumberLong(1422030960), "usage_ratio" : 0.015415809117257595 }

找出usage_ratio的最大值命令是:

mongodbCluster3:PRIMARY> db.cpu_data.find().sort({"usage_ratio":-1}).limit(1){ "_id" : ObjectId("54c39abbacace71b1bd599ac"), "epoch_min" : NumberLong(1422039660), "usage_ratio" : 0.5287633538246155 }

最小值很簡單,僅需將-1換成1即可:

mongodbCluster3:PRIMARY> db.cpu_data.find().sort({"epoch_min":1}).limit(1){ "_id" : ObjectId("54c39358acace71b1bd20824"), "epoch_min" : NumberLong(1422028800), "usage_ratio" : 0.053253963589668274 }


相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.