简体   繁体   中英

mongon: Find the ID with the highest value in another column

Find the ID(s) with the highest current price.("Currently")

Here is a sample of the data:

{
    "_id": "1043817906",
    "Currently": 6.00
}

I just know the code

db.xxx.aggregate({ $group : { _id:null, max: { $max : "$Currently" }}});

In this way, the result is

{ "_id" : null, "max" : 18000 }

But i also want know the "_id" number (with maximum "Currently").

Thanks!

you could sort on currently field and return the first document:

db.collection.aggregate([
  {$sort: {"Currently": -1}},
  {$limit: 1},
  {$project: {max: "$Currently"}}
])

您还可以将find()方法与limit(1)使用,并对字段进行sort ,即:

db.xxx.find().sort({ "Currently": -1 }).limit(1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM