# Aggregation in ES like MongoDB

**URL:** <https://discuss.elastic.co/t/aggregation-in-es-like-mongodb/20243>\
**Category:** Elasticsearch\
**Created:** [October 14, 2014, 3:07pm UTC](https://discuss.elastic.co/t/aggregation-in-es-like-mongodb/20243 "2014-10-14T15:07:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maks\_Materkov](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@Maks\_Materkov](https://discuss.elastic.co/u/Maks_Materkov)\
**Post date:** [October 14, 2014, 3:07pm UTC](https://discuss.elastic.co/t/aggregation-in-es-like-mongodb/20243/1 "2014-10-14T15:07:21Z")

</div>

Hi all,

I have many documents like  
{'user': 1, 'type': 1},  
{'user': 2, 'type': 1},  
{'user': 2, 'type': 2},  
{'user': 3, 'type': 1},  
{'user': 4, 'type': 1} ...

Then, I have this aggregation in MongoDB aggregation pipeline:

db.events1.aggregate([  
{$group: {\_id: "$user", events:{$addToSet: '$type'}}},  
{$group: {\_id: "$events", counter: {$sum: 1}}},  
])

So, after first phase (aggregate by user), i have:  
user1-\> 1  
user2 -\> 1,2  
user3 -\> 1  
user4 -\> 1

And after second (aggregate by 'events' set and count):  
1 -\> 3  
1,2 -\> 1  
(this is the desired result)

Can I do something similar to this in ElasticSearch aggregation?

Tnanks.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/00e64f0e-f26f-4126-b5c0-10e583d8499b%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/00e64f0e-f26f-4126-b5c0-10e583d8499b%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:56am UTC](https://discuss.elastic.co/t/aggregation-in-es-like-mongodb/20243/2 "2017-07-06T00:56:19Z")

</div>


