You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MongoDB聚合中如何将星期、月份转为字符串格式?

要把MongoDB聚合中$dayOfWeek、$month返回的整数转成对应的星期/月份字符串,有几种实用方法,分场景给你说明:

方法1:MongoDB 5.0+ 用$dateToString(最简便)

MongoDB 5.0新增的$dateToString支持直接格式化日期为星期、月份名称,无需手动映射。常用格式符:

  • %a:缩写星期(如"周一")
  • %A:全称星期(如"星期一")
  • %b:缩写月份(如"11月"或"Nov",取决于数据库语言设置)
  • %B:全称月份(如"十一月"或"November")

针对你的示例数据,聚合代码可写为:

[
  {
    "$project": {
      "_id": 0,
      "weekday_full": {"$dateToString": {"format": "%A", "date": "$timestamp"}},
      "weekday_short": {"$dateToString": {"format": "%a", "date": "$timestamp"}},
      "month_full": {"$dateToString": {"format": "%B", "date": "$timestamp"}},
      "month_short": {"$dateToString": {"format": "%b", "date": "$timestamp"}}
    }
  }
]

中文环境下返回结果:

{
  "weekday_full": "星期一",
  "weekday_short": "周一",
  "month_full": "十一月",
  "month_short": "11月"
}

方法2:低版本MongoDB 用数组映射

如果你的MongoDB版本低于5.0,可通过$arrayElemAt配合预设数组实现整数到字符串的映射:

  • 注意$dayOfWeek返回值规则:1=周日,2=周一,…,7=周六,需用$subtract减1对应数组下标
  • $month返回1-12,对应数组下标0-11

聚合代码示例:

[
  {
    "$project": {
      "_id": 0,
      "weekday": {
        "$arrayElemAt": [
          ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
          {"$subtract": [{"$dayOfWeek": "$timestamp"}, 1]}
        ]
      },
      "month": {
        "$arrayElemAt": [
          ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
          {"$subtract": [{"$month": "$timestamp"}, 1]}
        ]
      }
    }
  }
]

针对你的示例数据,返回结果:

{ "weekday": "周一", "month": "十一月" }

方法3:用$switch自定义映射

如果需要更灵活的自定义命名(比如英文缩写、特殊称谓),可以用$switch逐个匹配整数:

[
  {
    "$project": {
      "_id": 0,
      "weekday": {
        "$switch": {
          "branches": [
            {"case": {"$eq": [{"$dayOfWeek": "$timestamp"}, 1]}, "then": "Sunday"},
            {"case": {"$eq": [{"$dayOfWeek": "$timestamp"}, 2]}, "then": "Monday"},
            {"case": {"$eq": [{"$dayOfWeek": "$timestamp"}, 3]}, "then": "Tuesday"},
            {"case": {"$eq": [{"$dayOfWeek": "$timestamp"}, 4]}, "then": "Wednesday"},
            {"case": {"$eq": [{"$dayOfWeek": "$timestamp"}, 5]}, "then": "Thursday"},
            {"case": {"$eq": [{"$dayOfWeek": "$timestamp"}, 6]}, "then": "Friday"},
            {"case": {"$eq": [{"$dayOfWeek": "$timestamp"}, 7]}, "then": "Saturday"}
          ],
          "default": "Unknown"
        }
      },
      "month": {
        "$switch": {
          "branches": [
            {"case": {"$eq": [{"$month": "$timestamp"}, 1]}, "then": "January"},
            {"case": {"$eq": [{"$month": "$timestamp"}, 2]}, "then": "February"},
            {"case": {"$eq": [{"$month": "$timestamp"}, 3]}, "then": "March"},
            {"case": {"$eq": [{"$month": "$timestamp"}, 4]}, "then": "April"},
            {"case": {"$eq": [{"$month": "$timestamp"}, 5]}, "then": "May"},
            {"case": {"$eq": [{"$month": "$timestamp"}, 6]}, "then": "June"},
            {"case": {"$eq": [{"$month": "$timestamp"}, 7]}, "then": "July"},
            {"case": {"$eq": [{"$month": "$timestamp"}, 8]}, "then": "August"},
            {"case": {"$eq": [{"$month": "$timestamp"}, 9]}, "then": "September"},
            {"case": {"$eq": [{"$month": "$timestamp"}, 10]}, "then": "October"},
            {"case": {"$eq": [{"$month": "$timestamp"}, 11]}, "then": "November"},
            {"case": {"$eq": [{"$month": "$timestamp"}, 12]}, "then": "December"}
          ],
          "default": "Unknown"
        }
      }
    }
  }
]

返回结果:

{ "weekday": "Monday", "month": "November" }

内容的提问来源于stack exchange,提问作者yenicead

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 04:31:04