在MongoDB中获得列的最高价值 [英] Getting the highest value of a column in MongoDB

查看:74
本文介绍了在MongoDB中获得列的最高价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻求一些帮助,以使mongo文档的列达到最高价值.我可以对其进行排序,并获得顶部/底部,但我敢肯定,有更好的方法可以做到这一点.

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 do it.

我尝试了以下操作(以及不同的组合):

I tried the following (and different combinations):

transactions.find("id" => x).max({"sellprice" => 0})

但是它总是抛出错误.除了排序和获取顶部/底部之外,还有什么好方法吗?

But it keeps throwing errors. What's a good way to do it besides sorting and getting the top/bottom?

谢谢!

推荐答案

max()不能像您期望的那样在SQL for Mongo中工作.在将来的版本中,这可能会有所改变,但到目前为止,max,min将主要用于内部用于分片的索引键.

max() does not work the way you would expect it to in SQL for Mongo. This is perhaps going to change in future versions but as of now, max,min are to be used with indexed keys primarily internally for sharding.

请参见 http://www.mongodb.org/display /DOCS/min + and + max + Query + Specifiers

不幸的是,到目前为止,获取最大值的唯一方法是对该集合desc对该值进行排序,并采用第一个.

Unfortunately for now the only way to get the max value is to sort the collection desc on that value and take the first.

transactions.find("id" => x).sort({"sellprice" => -1}).limit(1).first()

这篇关于在MongoDB中获得列的最高价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆