MongoDB 中的 ISODate 没有时间
ISODate in MongoDB without time
我想知道是否有办法更改 MongoDB 中的 ISODate 格式(从 shell 到 pymongo)以仅显示日期而不显示时间。我的实际 collection(示例)具有以下结构。
{
"_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”)。
提前致谢!
以 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
我想知道是否有办法更改 MongoDB 中的 ISODate 格式(从 shell 到 pymongo)以仅显示日期而不显示时间。我的实际 collection(示例)具有以下结构。
{
"_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”)。 提前致谢!
以 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