并没有发现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 }
时间: 2024-10-02 11:07:48