JohnHenry | 21 Mar 09:49 2012

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

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 | 21 Mar 09:51 2012
Picon

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

You can simply find().sort({fieldName:1/-1}).limit(-1).
-----Original Message-----
From: JohnHenry <qhlonline@...>
Sender: mongodb-user@...
Date: Wed, 21 Mar 2012 01:49:21 
To: mongodb-user<mongodb-user@...>
Reply-To: mongodb-user@...
Subject: [mongodb-user] How to get max/min value of  an item in mongo collection?

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

--

-- 
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongodb-user@...
To unsubscribe from this group, send email to mongodb-user+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

Adam C | 21 Mar 11:31 2012

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

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

On Wednesday, March 21, 2012 8:49:21 AM UTC, JohnHenry wrote:
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

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mongodb-user/-/r7IxtAoYLgYJ.
To post to this group, send email to mongodb-user-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to mongodb-user+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
JohnHenry | 23 Mar 08:36 2012

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

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

On 3月21日, 下午6时31分, Adam C <ad...@...> wrote:
> 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
>
>
>
> On Wednesday, March 21, 2012 8:49:21 AM UTC, JohnHenry wrote:
>
> > 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- 隐藏被引用文字 -
>
> - 显示引用的文字 -

--

-- 
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongodb-user@...
To unsubscribe from this group, send email to mongodb-user+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

Nat | 23 Mar 08:39 2012
Picon

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

Sort with limit is not costly given you have an index on that column.
-----Original Message-----
From: JohnHenry <qhlonline@...>
Sender: mongodb-user@...
Date: Fri, 23 Mar 2012 00:36:57 
To: mongodb-user<mongodb-user@...>
Reply-To: mongodb-user@...
Subject: [mongodb-user] Re: How to get max/min value of an item in mongo collection?

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

On 3月21日, 下午6时31分, Adam C <ad...@...> wrote:
> 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
>
>
>
> On Wednesday, March 21, 2012 8:49:21 AM UTC, JohnHenry wrote:
>
> > 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- 隐藏被引用文字 -
>
> - 显示引用的文字 -

-- 
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongodb-user@...
To unsubscribe from this group, send email to mongodb-user+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

--

-- 
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongodb-user@...
To unsubscribe from this group, send email to mongodb-user+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

JohnHenry | 24 Mar 02:13 2012

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

But mongo support index on only one column of a collection, isn't it?

On 3月23日, 下午3时39分, "Nat" <nat.lu...@...> wrote:
> Sort with limit is not costly given you have an index on that column.
>
>
>
> -----Original Message-----
> From:JohnHenry<qhlonl...@...>
> Sender: mongodb-user@...
> Date: Fri, 23 Mar 2012 00:36:57
> To: mongodb-user<mongodb-user@...>
> Reply-To: mongodb-user@...
> Subject: [mongodb-user] Re: How to get max/min value of an item in mongo collection?
>
> 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
>
> On 3月21日, 下午6时31分, Adam C <ad...@...> wrote:
> > 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
>
> > On Wednesday, March 21, 2012 8:49:21 AM UTC,JohnHenrywrote:
>
> > > 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- 隐藏被引用文字 -
>
> > - 显示引用的文字 -
>
> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongodb-user@...
> To unsubscribe from this group, send email to mongodb-user+unsubscribe <at> googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/mongodb-user?hl=en.-
隐藏被引用文字 -
>
> - 显示引用的文字 -

--

-- 
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongodb-user@...
To unsubscribe from this group, send email to mongodb-user+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

H.J | 24 Mar 02:33 2012
Picon

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

Compound key index is for more than one fields.
http://www.mongodb.org/display/DOCS/Indexes#Indexes-CompoundKeys

在 2012年3月24日 上午9:13,JohnHenry <qhlonline-9Onoh4P/yGk@public.gmane.org>写道:
But mongo support index on only one column of a collection, isn't it?

On 3月23日, 下午3时39分, "Nat" <nat.lu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> Sort with limit is not costly given you have an index on that column.
>
>
>
> -----Original Message-----
> From:JohnHenry<qhlonl...-9Onoh4P/yGk@public.gmane.org>
> Sender: mongodb-user <at> googlegroups.com
> Date: Fri, 23 Mar 2012 00:36:57
> To: mongodb-user<mongodb-user-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
> Reply-To: mongodb-user <at> googlegroups.com
> Subject: [mongodb-user] Re: How to get max/min value of an item in mongo collection?
>
> 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
>
> On 3月21日, 下午6时31分, Adam C <ad...-Ot75HdpNzd8AvxtiuMwx3w@public.gmane.org> wrote:
> > 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
>
> > On Wednesday, March 21, 2012 8:49:21 AM UTC,JohnHenrywrote:
>
> > > 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- 隐藏被引用文字 -
>
> > - 显示引用的文字 -
>
> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongodb-user-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
> To unsubscribe from this group, send email to mongodb-user+unsubscribe <at> googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/mongodb-user?hl=en.- 隐藏被引用文字 -
>
> - 显示引用的文字 -

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
To unsubscribe from this group, send email to mongodb-user+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.




--
尽人事,听天命!

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to mongodb-user+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
JohnHenry | 27 Mar 11:25 2012

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

Thank you very much!

On 3月24日, 上午9时33分, "H.J" <shiyi...@...> wrote:
> Compound key index is for more than one fields.http://www.mongodb.org/display/DOCS/Indexes#Indexes-CompoundKeys
>
> 在 2012年3月24日 上午9:13,JohnHenry <qhlonl... <at> 163.com>写道:
>
>
>
>
>
> > But mongo support index on only one column of a collection, isn't it?
>
> > On 3月23日, 下午3时39分, "Nat" <nat.lu... <at> gmail.com> wrote:
> > > Sort with limit is not costly given you have an index on that column.
>
> > > -----Original Message-----
> > > From:JohnHenry<qhlonl...@...>
> > > Sender: mongodb-user@...
> > > Date: Fri, 23 Mar 2012 00:36:57
> > > To: mongodb-user<mongodb-user@...>
> > > Reply-To: mongodb-user@...
> > > Subject: [mongodb-user] Re: How to get max/min value of an item in mongo
> > collection?
>
> > > 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
>
> > > On 3月21日, 下午6时31分, Adam C <ad... <at> 10gen.com> wrote:
> > > > 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
>
> > > > On Wednesday, March 21, 2012 8:49:21 AM UTC,JohnHenrywrote:
>
> > > > > 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- 隐藏被引用文字 -
>
> > > > - 显示引用的文字 -
>
> > > --
> > > You received this message because you are subscribed to the Google
> > Groups "mongodb-user" group.
> > > To post to this group, send email to mongodb-user@...
> > > To unsubscribe from this group, send email to
> > mongodb-user+unsubscribe@...
> > > For more options, visit this group athttp://
> > groups.google.com/group/mongodb-user?hl=en.- 隐藏被引用文字 -
>
> > > - 显示引用的文字 -
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "mongodb-user" group.
> > To post to this group, send email to mongodb-user@...
> > To unsubscribe from this group, send email to
> > mongodb-user+unsubscribe@...
> > For more options, visit this group at
> >http://groups.google.com/group/mongodb-user?hl=en.
>
> --
> 尽人事,听天命!- 隐藏被引用文字 -
>
> - 显示引用的文字 -

--

-- 
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongodb-user@...
To unsubscribe from this group, send email to mongodb-user+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

Max Schireson | 7 Apr 15:53 2012
Picon

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

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

On Mar 24, 2012 3:13 AM, "JohnHenry" <qhlonline-9Onoh4P/yGk@public.gmane.org> wrote:
But mongo support index on only one column of a collection, isn't it?

On 3月23日, 下午3时39分, "Nat" <nat.lu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> Sort with limit is not costly given you have an index on that column.
>
>
>
> -----Original Message-----
> From:JohnHenry<qhlonl... <at> 163.com>
> Sender: mongodb-user <at> googlegroups.com
> Date: Fri, 23 Mar 2012 00:36:57
> To: mongodb-user<mongodb-user-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
> Reply-To: mongodb-user <at> googlegroups.com
> Subject: [mongodb-user] Re: How to get max/min value of an item in mongo collection?
>
> 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
>
> On 3月21日, 下午6时31分, Adam C <ad...-Ot75HdpNzd8AvxtiuMwx3w@public.gmane.org> wrote:
> > 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
>
> > On Wednesday, March 21, 2012 8:49:21 AM UTC,JohnHenrywrote:
>
> > > 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- 隐藏被引用文字 -
>
> > - 显示引用的文字 -
>
> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongodb-user-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
> To unsubscribe from this group, send email to mongodb-user+unsubscribe <at> googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/mongodb-user?hl=en.- 隐藏被引用文字 -
>
> - 显示引用的文字 -

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
To unsubscribe from this group, send email to mongodb-user+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to mongodb-user+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
JohnHenry | 7 Apr 15:40 2012

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

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

On 3月23日, 下午3时36分, JohnHenry <qhlonl... <at> 163.com> wrote:
> 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
>
> On 3月21日, 下午6时31分, Adam C <ad...@...> wrote:
>
>
>
> > 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
>
> > On Wednesday, March 21, 2012 8:49:21 AM UTC,JohnHenrywrote:
>
> > > 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- 隐藏被引用文字 -
>
> > - 显示引用的文字 -- 隐藏被引用文字 -
>
> - 显示引用的文字 -

--

-- 
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongodb-user@...
To unsubscribe from this group, send email to mongodb-user+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Gmane