Getting the highest value of a column in MongoDB

后端 未结 9 1649
粉色の甜心
粉色の甜心 2020-12-05 02:44

I\'ve been for some help on getting the highest value on a column for a mongo document. I can sort it and get the top/bottom, but I\'m pretty sure there is a better way to d

9条回答
  •  清歌不尽
    2020-12-05 03:21

    Use aggregate():

    db.transactions.aggregate([
      {$match: {id: x}},
      {$sort: {sellprice:-1}},
      {$limit: 1},
      {$project: {sellprice: 1}}
    ]);
    

提交回复
热议问题