Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion $group and non-existing fields

Received: by 10.224.186.20 with SMTP id cq20mr16039274qab.8.1351532445833;
        Mon, 29 Oct 2012 10:40:45 -0700 (PDT)
X-BeenThere: mongodb-user@googlegroups.com
Received: by 10.229.197.162 with SMTP id ek34ls2446734qcb.1.gmail; Mon, 29 Oct
 2012 10:40:31 -0700 (PDT)
Received: by 10.224.202.202 with SMTP id ff10mr1303233qab.0.1351532431526;
        Mon, 29 Oct 2012 10:40:31 -0700 (PDT)
Date: Mon, 29 Oct 2012 10:40:31 -0700 (PDT)
From: Jeremy Mikola <jmik...@gmail.com>
To: mongodb-user@googlegroups.com
Message-Id: <93595eb8-8f9d-4bd4-b47d-7b2a35d1841b@googlegroups.com>
In-Reply-To: <9d2b829a-5b55-462f-8f95-15130a3e6d74@googlegroups.com>
References: <be6a9aac-955a-4972-837e-110fb992d4bf@googlegroups.com>
 <01deb0b2-c776-4930-b065-122ff05cc061@googlegroups.com>
 <9d2b829a-5b55-462f-8f95-15130a3e6d74@googlegroups.com>
Subject: Re: $group and non-existing fields
MIME-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_768_19193565.1351532431156"

------=_Part_768_19193565.1351532431156
Content-Type: multipart/alternative; 
	boundary="----=_Part_769_27499379.1351532431156"

------=_Part_769_27499379.1351532431156
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit


On Monday, October 29, 2012 1:14:42 PM UTC-4, Fred wrote:
>
>
> Thx for help! So, now I have>
>
> mongo> db.test.aggregate({$group:{_id:'$group', maxAge:{$max:'$age'}}})
> {
>         "result" : [
>                 {
>                         "_id" : "bar",
>                         "maxAge" : 50
>                 },
>                 {
>                         "_id" : "admin",
>                         "maxAge" : 42
>                 }
>         ],
>         "ok" : 1
> }
>
> The oldest user from each group. Question is, how do I add additional 
> fields to the output like 'user' or how can I output the hole document 
> founded by $max?  How do I find second first and oldest users from each 
> group? Is this possible?
>


If you were to $sort the documents by age descending prior to $group, you 
could then $group by your "group" field ("admin" and "bar" buckets) and use 
the $first expression in $group to collect the first document you encounter.

I don't know of a way to access something that isn't the $first or $last 
document. At the expense of making the command result much larger, you 
could collect each grouped document with $push or $addToSet. In that case, 
you would benefit from a conservative projection to limit the document to 
just essential fields before collecting them.

If you have frequent need to query for offsets (i.e. skips) among ages for 
groups, it may be preferable to use the aggregation framework or the 
distinct() command to collect unique groups and then issue normal queries 
with a skip and limit. The optimal index in that case would be a compound 
index of group and age (descending or ascending depending on your needs).

------=_Part_769_27499379.1351532431156
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

<br>On Monday, October 29, 2012 1:14:42 PM UTC-4, Fred wrote:<blockquote cl=
ass=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px =
#ccc solid;padding-left: 1ex;"><br><div>Thx for help! So, now I have&gt;<br=
><br>mongo&gt; db.test.aggregate({$group:{_<wbr>id:'$group', maxAge:{$max:'=
$age'}}})<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "result" : [<b=
r>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp; "_id" : "bar",<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp; "maxAge" : 50<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; },<br>&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {=
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "_id" :=
 "admin",<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
; "maxAge" : 42<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp; ],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "ok" : 1<br>}<br><=
br>The oldest user from each group. Question is, how do I add additional fi=
elds to the output like 'user' or how can I output the hole document founde=
d by $max?&nbsp; How do I find second first and oldest users from each grou=
p? Is this possible?<br></div></blockquote><div><br><br>If you were to $sor=
t the documents by age descending prior to $group, you could then $group by=
 your "group" field ("admin" and "bar" buckets) and use the $first expressi=
on in $group to collect the first document you encounter.<br><br>I don't kn=
ow of a way to access something that isn't the $first or $last document. At=
 the expense of making the command result much larger, you could collect ea=
ch grouped document with $push or $addToSet. In that case, you would benefi=
t from a conservative projection to limit the document to just essential fi=
elds before collecting them.<br><br>If you have frequent need to query for =
offsets (i.e. skips) among ages for groups, it may be preferable to use the=
 aggregation framework or the distinct() command to collect unique groups a=
nd then issue normal queries with a skip and limit. The optimal index in th=
at case would be a compound index of group and age (descending or ascending=
 depending on your needs).<br></div>
------=_Part_769_27499379.1351532431156--

------=_Part_768_19193565.1351532431156--