Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB:获取子文档中的最大日期并保持文档结构_Mongodb_Aggregation Framework - Fatal编程技术网

MongoDB:获取子文档中的最大日期并保持文档结构

MongoDB:获取子文档中的最大日期并保持文档结构,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我有如下结构的文档: _id: 5d090529397f79d2fefc57fb, result: 23232, temperatures: { temp_limits: { min: 10, max: 31, }, temp_cities: [ { city: 'citie_1', city_te

我有如下结构的文档:

_id: 5d090529397f79d2fefc57fb,    
result: 23232,
temperatures: { 
    temp_limits: { 
        min: 10,
        max: 31,            
    },
    temp_cities:
        [ 
            { 
                city: 'citie_1',
                city_temp: [ 
                    { temp: 17, date: 2018-12-18T10:35:07.000Z}, // I want this
                    { temp: 21, date: 2018-12-17T11:35:05.000Z},
                    { temp: 23, date: 2018-12-17T14:36:07.000Z},
                ],
                locked: false
            },
            { 
                city: 'citie_2',
                city_temp: [ 
                    { temp: 15, date: 2018-12-18T14:15:07.000Z}, // and this
                    { temp: 22, date: 2018-12-17T11:33:02.000Z}                        
                ],
                locked: false
            }
    ]
}
_id: 5d090529397f79d2fefc57fb,    
result: 23232,
temperatures: { 
    temp_limits: { 
        min: 10,
        max: 31,            
    },
    temp_cities:
        [ 
            { 
                city: 'citie_1',
                city_temp: { temp: 17, date: 2018-12-18T10:35:07.000Z}                    
                locked: false
            },
            { 
                city: 'citie_2',
                city_temp: { temp: 15, date: 2018-12-18T14:15:07.000Z}
                locked: false
            }
    ]
}
我希望每个城市的最大
日期
和他的
温度
,但保持相同的结构。大概是这样的:

_id: 5d090529397f79d2fefc57fb,    
result: 23232,
temperatures: { 
    temp_limits: { 
        min: 10,
        max: 31,            
    },
    temp_cities:
        [ 
            { 
                city: 'citie_1',
                city_temp: [ 
                    { temp: 17, date: 2018-12-18T10:35:07.000Z}, // I want this
                    { temp: 21, date: 2018-12-17T11:35:05.000Z},
                    { temp: 23, date: 2018-12-17T14:36:07.000Z},
                ],
                locked: false
            },
            { 
                city: 'citie_2',
                city_temp: [ 
                    { temp: 15, date: 2018-12-18T14:15:07.000Z}, // and this
                    { temp: 22, date: 2018-12-17T11:33:02.000Z}                        
                ],
                locked: false
            }
    ]
}
_id: 5d090529397f79d2fefc57fb,    
result: 23232,
temperatures: { 
    temp_limits: { 
        min: 10,
        max: 31,            
    },
    temp_cities:
        [ 
            { 
                city: 'citie_1',
                city_temp: { temp: 17, date: 2018-12-18T10:35:07.000Z}                    
                locked: false
            },
            { 
                city: 'citie_2',
                city_temp: { temp: 15, date: 2018-12-18T14:15:07.000Z}
                locked: false
            }
    ]
}
我尝试了这一点,但我得到了不同的结果或不同的结构:

 {$unwind: '$temperatures.temp_cities'},
 {$unwind: '$temperatures.temp_cities.city_temp'},
 {$sort: { '$temperatures.temp_cities.city_temp.date': -1 } },
 {   
     $group: {
         _id: {
             _id: '$_id',
             bookie: "$temperatures.temp_cities.city"
         },                
         result: { $first: '$result' },             
         temperatures: {
             $first: "$temperatures.temp_cities.city_temp"
         }
     }
 }
可以将运算符与嵌套运算符一起使用。可使用以下运算符获取最大日期:

db.collection.aggregate([
    {
        $addFields: {
            "temperatures.temp_cities": {
                $map: {
                    input: "$temperatures.temp_cities",
                    as: "tc",
                    in: {
                        locked: "$$tc.locked",
                        city: "$$tc.city",
                        city_temp: {
                            $let: {
                                vars: { maxDate: { $max: "$$tc.city_temp.date" } },
                                in: {
                                    $arrayElemAt: [
                                        { $filter: { input: "$$tc.city_temp", cond: { $eq: [ "$$this.date", "$$maxDate" ] } } }, 0
                                    ]
                                }
                            }
                        }
                    }
                }
            }
        }
    }
])

顺序是重要的还是唯一的结构?必须保持相同的结构,元素的顺序无关紧要