【问题标题】:ISODate in MongoDB without timeMongoDB中的ISODate没有时间
【发布时间】:2019-11-27 14:59:11
【问题描述】:

我想知道是否有一种方法可以更改 MongoDB 中的 ISODate 格式(从 shell 到 pymongo)以仅显示日期而不显示时间。我的实际集合(示例)具有以下结构。

{
        "_id" : ObjectId("5dde89f8d93f9eca2f7aa135"),
        "worker_id" : "WORK_1",
        "gender" : "M",
        "birth" : ISODate("1970-06-08T00:00:00Z"),
        "job" : {
                "att" : [
                        {
                                "employer_id" : "EMPL_2",
                                "date" : ISODate("1990-05-05T00:00:00Z"),
                                "job_type" : "Att"
                        },
                        {
                                "employer_id" : "EMPL_1",
                                "date" : ISODate("1993-11-24T00:00:00Z"),
                                "job_type" : "Att"
                        }
                ],
                "mis" : [
                        {
                                "employer_id" : "EMPL_1",
                                "date" : ISODate("1991-12-01T00:00:00Z"),
                                "job_type" : "Mis"
                        }
                ]
        }
}
{
        "_id" : ObjectId("5dde89f8d93f9eca2f7aa136"),
        "worker_id" : "WORK_2",
        "gender" : "F",
        "birth" : ISODate("1988-08-22T00:00:00Z"),
        "job" : {
                "att" : [
                        {
                                "employer_id" : "EMPL_3",
                                "date" : ISODate("1995-08-30T00:00:00Z"),
                                "job_type" : "Att"
                        },
                        {
                                "employer_id" : "EMPL_3",
                                "date" : ISODate("1994-03-28T00:00:00Z"),
                                "job_type" : "Att"
                        }
                ],
                "mis" : [
                        {
                                "employer_id" : "EMPL_3",
                                "date" : ISODate("1992-01-01T00:00:00Z"),
                                "job_type" : "Mis"
                        }
                ]
        }
}
{
        "_id" : ObjectId("5dde89f8d93f9eca2f7aa137"),
        "worker_id" : "WORK_3",
        "gender" : "F",
        "birth" : ISODate("1978-05-24T00:00:00Z"),
        "job" : {
                "att" : null,
                "mis" : [
                        {
                                "employer_id" : "EMPL_1",
                                "date" : ISODate("1995-04-25T00:00:00Z"),
                                "job_type" : "Mis"
                        }
                ]
        }
}

最初我有格式为“%Y-%m-%d”(例如“1990-05-05”)的字符串数据。 提前谢谢!

【问题讨论】:

    标签: mongodb isodate


    【解决方案1】:

    将日期保存为 ISODate 格式是有意义的;您只需要稍微小心添加和提取日期。

    添加数据时使用datetime.datetime.combine()去除时间分量,使用datetime.date()将其转换回日期。

    from pymongo import MongoClient
    import datetime
    
    db = MongoClient()["mydatabase"]
    
    def utc_midnight(d: datetime.date) -> datetime.datetime:
        return datetime.datetime.combine(d, datetime.time(0, 0))
    
    db.testcollection.insert_one(
        {
            "worker_id": "WORK_1",
            "gender": "M",
            "birth": utc_midnight(datetime.date(1970, 6, 8)),
            "job": {
                "att": [
                    {
                        "employer_id": "EMPL_2",
                        "date": utc_midnight(datetime.date(1990, 5, 5)),
                        "job_type": "Att"
                    },
                    {
                        "employer_id": "EMPL_1",
                        "date": utc_midnight(datetime.date(1993, 11, 24)),
                        "job_type": "Att"
                    }
                ],
                "mis": [
                    {
                        "employer_id": "EMPL_1",
                        "date": utc_midnight(datetime.date(1991, 12, 1)),
                        "job_type": "Mis"
                    }
                ]
            }
        })
    
    
    record = db.testcollection.find_one()
    birth_date = record['birth'].date()
    print(birth_date.year)
    print(birth_date.month)
    print(birth_date.day)
    

    给予

    1970
    6
    8
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-09
      • 1970-01-01
      • 2018-02-22
      • 1970-01-01
      • 1970-01-01
      • 2018-05-10
      • 1970-01-01
      相关资源
      最近更新 更多