On this page本页内容
$densify
New in version 5.1.在版本5.1中新增。
Creates new documents in a sequence of documents where certain values in a field are missing.在缺少字段中某些值的文档序列中创建新文档。
You can use 您可以使用$densify to:$densify来:
The $densify stage has this syntax:$densify阶段具有以下语法:
{
$densify: {
field: <fieldName>,
partitionByFields: [ <field 1>, <field 2> ... <field n> ],
range: {
step: <number>,
unit: <time unit>,
bounds: < "full" || "partition" > || [ < lower bound >, < upper bound > ]
}
}
}
The $densify stage takes a document with these fields:$densify阶段获取包含以下字段的文档:
| Necessity | ||
|---|---|---|
field |
| |
partitionByFields |
| |
range |
| |
range.bounds |
| |
range.step |
| |
range.unit |
|
fieldFor documents that contain the specified field, 对于包含指定字段的文档,如果出现以下情况,$densify errors if:$densify将出错:
field value of type date and the unit field is not specified.field值,并且未指定单位字段。field value of type numeric and the unit field is specified.field值,并且指定了单位字段。field name begins with $. field名以$开头。$project.$project。partitionByFields如果$densify errors if any field name in the partitionByFields array:partitionByFields数组中有任何字段名出现以下情况,$densify将出错:
$.$开始。range.boundsIf range.bounds is an array:如果range.bounds是一个数组:
$densify$densify does not guarantee sort order of the documents it outputs.不保证输出文档的排序顺序。
To guarantee sort order, use 要保证排序顺序,请在要排序的字段上使用$sort on the field you want to sort by.$sort。
Create a 创建一个weather collection that contains temperature readings over four hour intervals.weather集合,包含四小时间隔的温度读数。
db.weather.insertMany( [
{
"metadata": { "sensorId": 5578, "type": "temperature" },
"timestamp": ISODate("2021-05-18T00:00:00.000Z"),
"temp": 12
},
{
"metadata": { "sensorId": 5578, "type": "temperature" },
"timestamp": ISODate("2021-05-18T04:00:00.000Z"),
"temp": 11
},
{
"metadata": { "sensorId": 5578, "type": "temperature" },
"timestamp": ISODate("2021-05-18T08:00:00.000Z"),
"temp": 11
},
{
"metadata": { "sensorId": 5578, "type": "temperature" },
"timestamp": ISODate("2021-05-18T12:00:00.000Z"),
"temp": 12
}
] )
This example uses the 本示例使用$densify stage to fill in the gaps between the four-hour intervals to achieve hourly granularity for the data points:$densify阶段填充四个小时间隔之间的间隙,以实现数据点的每小时粒度:
db.weather.aggregate( [
{
$densify: {
field: "timestamp",
range: {
step: 1,
unit: "hour",
bounds:[ ISODate("2021-05-18T00:00:00.000Z"), ISODate("2021-05-18T08:00:00.000Z") ]
}
}
}
] )
In the example:在该示例中:
The $densify stage fills in the gaps of time in between the recorded temperatures.$density阶段填补了记录温度之间的时间间隙。
field: "timestamp"timestamp field.timestamp字段。range:
step: 1timestamp field by 1 unit.timestamp字段递增1个单位。unit: hourtimestamp field by the hour.timestamp字段。bounds: [ ISODate("2021-05-18T00:00:00.000Z"), ISODate("2021-05-18T08:00:00.000Z") ]In the following output, the 在以下输出中,$densify stage fills in the gaps of time between the hours of 00:00:00 and 08:00:00.$densify阶段将填充00:00:00和08:00:00之间的时间间隔。
[
{
_id: ObjectId("618c207c63056cfad0ca4309"),
metadata: { sensorId: 5578, type: 'temperature' },
timestamp: ISODate("2021-05-18T00:00:00.000Z"),
temp: 12
},
{ timestamp: ISODate("2021-05-18T01:00:00.000Z") },
{ timestamp: ISODate("2021-05-18T02:00:00.000Z") },
{ timestamp: ISODate("2021-05-18T03:00:00.000Z") },
{
_id: ObjectId("618c207c63056cfad0ca430a"),
metadata: { sensorId: 5578, type: 'temperature' },
timestamp: ISODate("2021-05-18T04:00:00.000Z"),
temp: 11
},
{ timestamp: ISODate("2021-05-18T05:00:00.000Z") },
{ timestamp: ISODate("2021-05-18T06:00:00.000Z") },
{ timestamp: ISODate("2021-05-18T07:00:00.000Z") },
{
_id: ObjectId("618c207c63056cfad0ca430b"),
metadata: { sensorId: 5578, type: 'temperature' },
timestamp: ISODate("2021-05-18T08:00:00.000Z"),
temp: 11
}
{
_id: ObjectId("618c207c63056cfad0ca430c"),
metadata: { sensorId: 5578, type: 'temperature' },
timestamp: ISODate("2021-05-18T12:00:00.000Z"),
temp: 12
}
]
Create a 创建包含两种咖啡豆数据的coffee collection that contains data for two varieties of coffee beans:coffee集合:
db.coffee.insertMany( [
{
"altitude": 600,
"variety": "Arabica Typica",
"score": 68.3
},
{
"altitude": 750,
"variety": "Arabica Typica",
"score": 69.5
},
{
"altitude": 950,
"variety": "Arabica Typica",
"score": 70.5
},
{
"altitude": 1250,
"variety": "Gesha",
"score": 88.15
},
{
"altitude": 1700,
"variety": "Gesha",
"score": 95.5,
"price": 1029
}
] )
This example uses 本示例使用$densify to densify the altitude field for each coffee variety:$densify为每个咖啡variety(品种)加密altitude(海拔)字段:
db.coffee.aggregate( [
{
$densify: {
field: "altitude",
partitionByFields: [ "variety" ],
range: {
bounds: "full",
step: 200
}
}
}
] )
The example aggregation:示例聚合:
variety to create one grouping for Arabica Typica and one for Gesha coffee.variety划分文档,为Arabica Typica和Gesha咖啡创建一个分组。full range, meaning that the data is densified across the full range of existing documents for each partition.full范围,这意味着数据在每个分区的现有文档的整个范围内进行加密。step of 200, meaning new documents are created at altitude intervals of 200.step为200,表示以200的altitude(海拔)间隔创建新文档。The aggregation outputs the following documents:聚合输出以下文件:
[
{
_id: ObjectId("618c031814fbe03334480475"),
altitude: 600,
variety: 'Arabica Typica',
score: 68.3
},
{
_id: ObjectId("618c031814fbe03334480476"),
altitude: 750,
variety: 'Arabica Typica',
score: 69.5
},
{ variety: 'Arabica Typica', altitude: 800 },
{
_id: ObjectId("618c031814fbe03334480477"),
altitude: 950,
variety: 'Arabica Typica',
score: 70.5
},
{ variety: 'Gesha', altitude: 600 },
{ variety: 'Gesha', altitude: 800 },
{ variety: 'Gesha', altitude: 1000 },
{ variety: 'Gesha', altitude: 1200 },
{
_id: ObjectId("618c031814fbe03334480478"),
altitude: 1250,
variety: 'Gesha',
score: 88.15
},
{ variety: 'Gesha', altitude: 1400 },
{ variety: 'Gesha', altitude: 1600 },
{
_id: ObjectId("618c031814fbe03334480479"),
altitude: 1700,
variety: 'Gesha',
score: 95.5,
price: 1029
},
{ variety: 'Arabica Typica', altitude: 1000 },
{ variety: 'Arabica Typica', altitude: 1200 },
{ variety: 'Arabica Typica', altitude: 1400 },
{ variety: 'Arabica Typica', altitude: 1600 }
]
This image visualizes the documents created with 此图像显示了使用$densify:$densify创建的文档:

$densify.$densify创建的文档。This example uses 本示例使用$densify to only densify gaps in the altitude field within each variety:$densify仅加密每个variety内altitude字段中的间隙:
db.coffee.aggregate( [
{
$densify: {
field: "altitude",
partitionByFields: [ "variety" ],
range: {
bounds: "partition",
step: 200
}
}
}
] )
The example aggregation:示例聚合:
variety to create one grouping for Arabica Typica and one for Gesha coffee.variety划分文档,为Arabica Typica和Gesha咖啡创建一个分组。Specifies a 指定partition range, meaning that the data is densified within each partition.partition范围,这意味着数据在每个分区内加密。
Arabica Typica partition, the range is 600-900.Arabica Typica分区,范围为600-900。Gesha partition, the range is 1250-1700.Gesha分区,范围为1250-1700。step of 200, meaning new documents are created at altitude intervals of 200.step为200,表示以200的altitude间隔创建新文档。The aggregation outputs the following documents:聚合输出以下文件:
[
{
_id: ObjectId("618c031814fbe03334480475"),
altitude: 600,
variety: 'Arabica Typica',
score: 68.3
},
{
_id: ObjectId("618c031814fbe03334480476"),
altitude: 750,
variety: 'Arabica Typica',
score: 69.5
},
{ variety: 'Arabica Typica', altitude: 800 },
{
_id: ObjectId("618c031814fbe03334480477"),
altitude: 950,
variety: 'Arabica Typica',
score: 70.5
},
{
_id: ObjectId("618c031814fbe03334480478"),
altitude: 1250,
variety: 'Gesha',
score: 88.15
},
{ variety: 'Gesha', altitude: 1450 },
{ variety: 'Gesha', altitude: 1650 },
{
_id: ObjectId("618c031814fbe03334480479"),
altitude: 1700,
variety: 'Gesha',
score: 95.5,
price: 1029
}
]
This image visualizes the documents created with 此图像显示了使用$densify:$densify创建的文档:

$densify.$densify创建的文档。