Data type conversion in MongoDB

后端 未结 5 1252
离开以前
离开以前 2020-12-29 04:16

I have a collection called Document in MongoDB. Documents in this collection have a field called CreationDate stored in ISO date type. My task is to count the number of docu

5条回答
  •  独厮守ぢ
    2020-12-29 04:47

    Another easier way you can convert ISODate to various date formats is to use the $dateToString aggregation operator.

    db.collection.aggregate([
        { $group: {
            _id: { $dateToString: { format: "%Y-%m-%d %H:%M", date: "$CreationDate" } },
            count: { $sum: 1 }
        }},
        { $sort : { count: -1 }}
    ])
    

提交回复
热议问题