MongoDB shell obtains the maximum and minimum values. mongodbshell
MongoDB does not find a dedicated method to calculate the maximum value, but it can be replaced by sorting and getting the first one.
The following collection data is as follows:
{ "_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 }
The command to find the maximum value of usage_ratio is:
mongodbCluster3:PRIMARY> db.cpu_data.find().sort({"usage_ratio":-1}).limit(1){ "_id" : ObjectId("54c39abbacace71b1bd599ac"), "epoch_min" : NumberLong(1422039660), "usage_ratio" : 0.5287633538246155 }
The minimum value is very simple. You only need to replace-1 with 1:
mongodbCluster3:PRIMARY> db.cpu_data.find().sort({"epoch_min":1}).limit(1){ "_id" : ObjectId("54c39358acace71b1bd20824"), "epoch_min" : NumberLong(1422028800), "usage_ratio" : 0.053253963589668274 }