Query ISODate on MongoDB with Google Sheets “dd-MM-yyyy HH:ss” as an output

会有一股神秘感。 提交于 2019-12-20 04:23:16

问题


I use a tool called Redash to query (in JSON) on MongoDB. In my collections dates are formulated in ISO, so when my query is imported (with google sheets' importdata function) to a sheet, I have to convert it to the appropriate format with a formula designed in the sheet. I would love to integrate this operation directly in my query, that the ISO date format is directly sent to Sheets in the appropriate "dd-MM-yyyy HH:ss" format.

Any ideas ?

Many many thanks


回答1:


You may be able to use the $dateToString aggregation operator inside a $project aggregation stage.

For example:

> db.test.find()
{ "_id": 0, "date": ISODate("2018-03-07T05:14:13.063Z"), "a": 1, "b": 2 }

> db.test.aggregate([
    {$project: {
        date: {$dateToString: {
            format: '%d-%m-%Y %H:%M:%S',
            date: '$date'
        }},
        a: '$a',
        b: '$b'
    }}
])
{ "_id": 0, "date": "07-03-2018 05:14:13", "a": 1, "b": 2 }

Note that although the $dateToString operator was available since MongoDB 3.0, MongoDB 3.6 adds the capability to output the string according to a specific timezone.



来源:https://stackoverflow.com/questions/49116680/query-isodate-on-mongodb-with-google-sheets-dd-mm-yyyy-hhss-as-an-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!