How to get max/min value of an item in mongo collection?

7,154 views
Skip to first unread message

JohnHenry

unread,
Mar 21, 2012, 4:49:21 AM3/21/12
to mongodb-user
Hi, all
Is there any way in mongo to directly return max value of some
field in an collection? I notice that mongo shell has such way, but I
don't konw whether the development API support this.

Regards

Nat

unread,
Mar 21, 2012, 4:51:23 AM3/21/12
to mongod...@googlegroups.com
You can simply find().sort({fieldName:1/-1}).limit(-1).
--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

Adam C

unread,
Mar 21, 2012, 6:31:16 AM3/21/12
to mongod...@googlegroups.com
Are you looking for a way to do this specifically using one of the drivers?

If so, which one?

If not, then Nat's reply should get you what you need.

Adam

JohnHenry

unread,
Mar 23, 2012, 3:36:57 AM3/23/12
to mongodb-user
Yes, I am using C++ drivers. And I do this by:

uids = dbConn.findOne(s_user_table,
BSON("$orderby"<<BSON("UID"<<-1)), &field_to_find);

But I thought that sort firstly will be somewhat costly. So I post
this question to get a direct method to find extreme points
> > Regards- 隐藏被引用文字 -
>
> - 显示引用的文字 -

Nat

unread,
Mar 23, 2012, 3:39:39 AM3/23/12
to mongod...@googlegroups.com
Sort with limit is not costly given you have an index on that column.
-----Original Message-----
From: JohnHenry <qhlo...@163.com>
Sender: mongod...@googlegroups.com
Date: Fri, 23 Mar 2012 00:36:57
To: mongodb-user<mongod...@googlegroups.com>
Reply-To: mongod...@googlegroups.com

JohnHenry

unread,
Mar 23, 2012, 9:13:29 PM3/23/12
to mongodb-user
But mongo support index on only one column of a collection, isn't it?
> For more options, visit this group athttp://groups.google.com/group/mongodb-user?hl=en.- 隐藏被引用文字 -
>
> - 显示引用的文字 -

H.J

unread,
Mar 23, 2012, 9:33:09 PM3/23/12
to mongod...@googlegroups.com
Compound key index is for more than one fields.
http://www.mongodb.org/display/DOCS/Indexes#Indexes-CompoundKeys
--
尽人事,听天命!

JohnHenry

unread,
Mar 27, 2012, 5:25:31 AM3/27/12
to mongodb-user
Thank you very much!

On 3月24日, 上午9时33分, "H.J" <shiyi...@gmail.com> wrote:
> Compound key index is for more than one fields.http://www.mongodb.org/display/DOCS/Indexes#Indexes-CompoundKeys
> 尽人事,听天命!- 隐藏被引用文字 -
>
> - 显示引用的文字 -

JohnHenry

unread,
Apr 7, 2012, 9:40:26 AM4/7/12
to mongodb-user
remedy
uids =dbConn.findOne(s_user_table, BSON("UID"<<BSON("$orderby"<<-1)));
> > - 显示引用的文字 -- 隐藏被引用文字 -
>
> - 显示引用的文字 -

Max Schireson

unread,
Apr 7, 2012, 9:53:44 AM4/7/12
to mongod...@googlegroups.com

No, you can define multiple indexes on a collectuon. I think the limit is 64.

You are right the sort will be slow without the index. But with the index it is very close to just directly returning the value.

Without an index you might try a map reduce - the map should emit the field, the reduce iterates through and updates the running maximum and returns it when done.

Obviously much much faster with an index.

-- Max

Reply all
Reply to author
Forward
0 new messages