The reason you have to do the $first as well is because when you group you
must choose what sub-group field you want.

This is essentially the same as GROUP in SQL but by default, depending on
what database you are using, it chooses the MAX of all other fields.

On 6 September 2013 08:52, ifynk85 wrote:

Hi all. I need help with $project pipeline operator. I have some document
like this:

{
'_id': '1231231',
'campaign':{
'id': '321321',
'title': 'some title'
}
}

I need group by 'campaign.id' and in result i need 'campaign.title'
field. My code:


$opts = array(
array('$match' => $condition),
array('$project' => array(
'campaign' => 1,
)),
array('$group' => array(
'_id' => '$campaign.id',
'title' => array('$first' => '$campaign.title'),
))
);

It's worked, but i am not sure in this line " 'title' => array('$first' =>
'$campaign.title'),"

When i tried " 'title' => '$campaign.title'," in $group operator i got
error "the group aggregate field 'title' must be defined as an expression
inside an object"

This code not worked to:

array('$project' => array(
'campaign' => 1,
'title' => '$campaign.title'
)),

No errors. But in result i cant see "title" field.

What is wrong? Thanks and sorry for my bad English.

--
--
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@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user+unsubscribe@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

---
You received this message because you are subscribed to the Google Groups
"mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to mongodb-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
--
--
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@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user+unsubscribe@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 2 of 3 | next ›
Discussion Overview
groupmongodb-user @
categoriesmongodb
postedSep 6, '13 at 7:52a
activeSep 6, '13 at 10:48a
posts3
users2
websitemongodb.org
irc#mongodb

2 users in discussion

Ifynk85: 2 posts Sam Millman: 1 post

People

Translate

site design / logo © 2022 Grokbase