- Reference >
- Operators >
- Aggregation Pipeline Stages >
- $densify (aggregation)
$densify (aggregation)¶
On this page
Definition¶
Syntax¶
The $densify
stage has this syntax:
The $densify
stage takes a document with these fields:
Field | Necessity | Description |
---|---|---|
field | Required | The field to densify. The values of the specified
Documents that do not contain the specified To specify a For restrictions, see field Restrictions. |
partitionByFields | Optional | The set of fields to act as the compound key to group
the documents. In the If you omit this field, For an example, see Densifiction with Partitions. For restrictions, see partitionByFields Restrictions. |
range | Required | An object that specifies how the data is densified. |
range.bounds | Required | You can specify
If
If
If
|
range.step | Required | The amount to increment the field value
in each document. If range.unit is specified, |
range.unit | Required if field is a date. | The unit to apply to the step field when incrementing date values in field. You can specify one of the following values for
For an example, see Densify Time Series Data. |
Behavior and Restrictions¶
field
Restrictions¶
For documents that contain the specified field,
$densify
errors if:
- Any document in the collection has a
field
value of type date and the unit field is not specified. - Any document in the collection has a
field
value of type numeric and the unit field is specified. - The
field
name begins with$
. You must rename the field if you want to densify it. To rename fields, use$project
.
partitionByFields
Restrictions¶
$densify
errors if any field name in the
partitionByFields array:
- Evaluates to a non-string value.
- Begins with
$
.
range.bounds
Behavior¶
If range.bounds is an array:
Examples¶
Densify Time Series Data¶
Create a weather
collection that contains temperature readings over
four hour intervals.
This example uses the $densify
stage to fill in the gaps
between the four-hour intervals to achieve hourly granularity for the
data points:
In the example:
- The
$densify
stage fills in the gaps of time in between the recorded temperatures.field: "timestamp"
densifies thetimestamp
field.range:
step: 1
increments thetimestamp
field by 1 unit.unit: hour
densifies thetimestamp
field by the hour.bounds: [ ISODate("2021-05-18T00:00:00.000Z"), ISODate("2021-05-18T08:00:00.000Z") ]
sets the range of time that is densified.
In the following output, the $densify
stage fills in the gaps of time
between the hours of 00:00:00
and 08:00:00
.
Densifiction with Partitions¶
Create a coffee
collection that contains data for two
varieties of coffee beans:
Densify the Full Range of Values¶
This example uses $densify
to densify the
altitude
field for each coffee variety
:
The example aggregation:
- Partitions the documents by
variety
to create one grouping forArabica Typica
and one forGesha
coffee. - Specifies a
full
range, meaning that the data is densified across the full range of existing documents for each partition. - Specifies a
step
of200
, meaning new documents are created ataltitude
intervals of200
.
The aggregation outputs the following documents:
This image visualizes the documents created with $densify
:

- The darker squares represent the original documents in the collection.
- The lighter squares represent the documents created with
$densify
.
Densify Values within Each Partition¶
This example uses $densify
to only densify gaps in the
altitude
field within each variety
:
The example aggregation:
- Partitions the documents by
variety
to create one grouping forArabica Typica
and one forGesha
coffee. - Specifies a
partition
range, meaning that the data is densified within each partition.- For the
Arabica Typica
partition, the range is600
-900
. - For the
Gesha
partition, the range is1250
-1700
.
- For the
- Specifies a
step
of200
, meaning new documents are created ataltitude
intervals of200
.
The aggregation outputs the following documents:
This image visualizes the documents created with $densify
:

- The darker squares represent the original documents in the collection.
- The lighter squares represent the documents created with
$densify
.