【问题标题】:MongoDB: get max date in subdocument and keep document structureMongoDB:获取子文档中的最大日期并保持文档结构
【发布时间】:2019-06-19 18:59:13
【问题描述】:

我的文档结构如下:

_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
            }
    ]
}

我想要每个city_temp 上的最大date 和他的temp,但保持相同的结构。像这样的:

_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"
         }
     }
 }

【问题讨论】:

  • 顺序重要还是结构重要?
  • 结构要保持一致,元素顺序无所谓

标签: mongodb aggregation-framework


【解决方案1】:

您可以将$map 运算符与嵌套的$filter 一起使用。最大日期可以使用$max操作符获取:

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
                                    ]
                                }
                            }
                        }
                    }
                }
            }
        }
    }
])

Mongo Playground

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-05
    • 2014-02-02
    • 2021-04-01
    • 2016-04-10
    • 2016-02-01
    • 2013-07-12
    相关资源
    最近更新 更多