我正在監視一些 AWS Batch 活動的 mongoDB 資料庫上撰寫查詢,但不幸的是該欄位的型別updated_at并不一致。
如果欄位status為submitted,則型別為datetime.datetime(例如:)2022-01-17T15:13:34.929 00:00,如果status為 ,succeeded則型別為time.time(例如:1642434736,自 1970 年 1 月 1 日以來的秒數)。
由于我不能直接將int數字轉換為日期(我需要雙精度或其他型別)[來源],我需要將日期轉換為數字,我是這樣做的:
db.topic_scorings.aggregate([
?{
?$addFields: {
?timeSeconds: {
?$convert: {
?input: "updated_at",
?to: "double",
?onNull: "$updated_at",
?onError: "$updated_at"
?}
?}
?}
?}
])
但問題是 type 的檔案int保持相同的值(從紀元開始的秒數,剛剛轉換為 type double),并且 type 的檔案datetime.datetime轉換為 double 型別,作為自 1970 年 1 月 1 日以來的毫秒
數。
所以我的問題是:
- 如何根據
updated_atField 的值乘以statusField ?
uj5u.com熱心網友回復:
這個怎么樣?它將數字轉換為日期,倍數 1000 將秒轉換為毫秒
db.topic_scorings.aggregate(
[
{
$project:
{
status: 1,
newDateField:
{
//$cond: [ { "$eq": [ "$status", "submitted" ] }, "$updated_at", { $convert : { input: "$updated_at", to: "long" } } ]
//$cond: [ { "$eq": [ "$status", "submitted" ] }, "$updated_at", { $toDate : { $convert : { input: "$updated_at", to: "long" } } }]
$cond: [
{ "$eq": [ "$status", "submitted" ] },
"$updated_at",
{ $toDate : { $multiply : [ {$convert : { input: "$updated_at", to: "long" } }, 1000 ] } }
]
}
}
}
]
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/417229.html
標籤:
