开发者

Is there any aggregation function to get weekday or month as string in mongodb?

开发者 https://www.devze.com 2022-12-07 17:43 出处:网络
Basically I want to get weekday and month values as string in mongodb, I found $dayOfWeek and $month functions under $project and they return integer values, how can I convert these integers to string

Basically I want to get weekday and month values as string in mongodb, I found $dayOfWeek and $month functions under $project and they return integer values, how can I convert these integers to string?

For example in sql-server, I can get day and month as string like the following:

DECLARE @dat开发者_运维知识库e datetime = '2019-11-11T13:28:22.300';   
SELECT DATENAME(WEEKDAY, @date) as weekday, DATENAME(month, @date) as month;

In mongodb, I have tried the following aggregation:

data:

_id: ObjectId('6384b63e48c7aa84d66ec31f') timestamp: 2019-11-11T13:28:22.300+00:00

then I created the following aggregation:

[
  {
    '$project': {
      '_id': 0, 
      'weekday': {
        '$dayOfWeek': '$timestamp'
      }, 
      'month': {
        '$month': '$timestamp'
      }
    }
  }
]

and the result is:

weekday: 2 month: 11

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号