- Aggregation >
- Aggregation Pipeline >
- Aggregation with User Preference Data
Aggregation with User Preference Data¶
On this page
Data Model¶
Consider a hypothetical sports club with a database that contains a
users
collection that tracks the user’s join dates, sport preferences,
and stores these data in documents that resemble the following:
Normalize and Sort Documents¶
The following operation returns user names in upper case and in
alphabetical order. The aggregation includes user names for all documents in
the users
collection. You might do this to normalize user names for
processing.
All documents from the users
collection pass through the
pipeline, which consists of the following operations:
The results of the aggregation would resemble the following:
Return Usernames Ordered by Join Month¶
The following aggregation operation returns user names sorted by the month they joined. This kind of aggregation could help generate membership renewal notices.
The pipeline passes all documents in the users
collection through
the following operations:
- The
$project
operator:- Creates two new fields:
month_joined
andname
. - Suppresses the
id
from the results. Theaggregate()
method includes the_id
, unless explicitly suppressed.
- Creates two new fields:
- The
$month
operator converts the values of thejoined
field to integer representations of the month. Then the$project
operator assigns those values to themonth_joined
field. - The
$sort
operator sorts the results by themonth_joined
field.
The operation returns results that resemble the following:
Return Total Number of Joins per Month¶
The following operation shows how many people joined each month of the year. You might use this aggregated data for recruiting and marketing strategies.
The pipeline passes all documents in the users
collection through
the following operations:
- The
$project
operator creates a new field calledmonth_joined
. - The
$month
operator converts the values of thejoined
field to integer representations of the month. Then the$project
operator assigns the values to themonth_joined
field. - The
$group
operator collects all documents with a givenmonth_joined
value and counts how many documents there are for that value. Specifically, for each unique value,$group
creates a new “per-month” document with two fields:_id
, which contains a nested document with themonth_joined
field and its value.number
, which is a generated field. The$sum
operator increments this field by 1 for every document containing the givenmonth_joined
value.
- The
$sort
operator sorts the documents created by$group
according to the contents of themonth_joined
field.
The result of this aggregation operation would resemble the following:
Return the Five Most Common “Likes”¶
The following aggregation collects top five most “liked” activities in the data set. This type of analysis could help inform planning and future development.
The pipeline begins with all documents in the users
collection,
and passes these documents through the following operations:
The
$unwind
operator separates each value in thelikes
array, and creates a new version of the source document for every element in the array.Example
Given the following document from the
users
collection:The
$unwind
operator would create the following documents:The
$group
operator collects all documents with the same value for thelikes
field and counts each grouping. With this information,$group
creates a new document with two fields:_id
, which contains thelikes
value.number
, which is a generated field. The$sum
operator increments this field by 1 for every document containing the givenlikes
value.
The
$sort
operator sorts these documents by thenumber
field in reverse order.The
$limit
operator only includes the first 5 result documents.
The results of aggregation would resemble the following: